package com.lz.modules.performance.controller;


import com.lz.common.utils.R;
import com.lz.modules.flow.entity.ResultModel;
import com.lz.modules.flow.entity.ResultTagetLib;
import com.lz.modules.flow.model.ResultModelDto;
import com.lz.modules.flow.model.ResultTagetLibDto;
import com.lz.modules.flow.req.ResultModelDetailReq;
import com.lz.modules.flow.req.ResultModelItemReq;
import com.lz.modules.flow.req.ResultTagetLibItemReq;
import com.lz.modules.flow.service.ResultModelService;
import com.lz.modules.performance.service.ResultTagetLibService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/resultModel")
@Api(tags = "考核模板相关")
@Slf4j
public class ResultModelController {


    @Autowired
    private ResultModelService resultModelService;

    @Autowired
    private ResultTagetLibService resultTagetLibService;




    @GetMapping("/getByGroupId")
    @ApiOperation(value="根据考核组id获取模板列表")
    @ApiResponses({@ApiResponse(code = 200,message = "成功",response = ResultModelDetailReq.class)})
    public R getByGroupId(@RequestParam @ApiParam("考核组ID") Long id) {

        //List<ResultModel> resultModels = resultModelService.selectResultModelByGroupId(id);
        ResultModelDetailReq resultModelDetailReq = new ResultModelDetailReq();
        List<ResultModelItemReq> modelItems = new ArrayList<>();
        List<ResultModelDto> resultModelDtos = resultModelService.selectResultDtoByGroupId(id);
        for (ResultModelDto dto:resultModelDtos
        ) {
            resultModelDetailReq.setCalculateId(dto.getCalculateId());
            resultModelDetailReq.setEvaluationGroupId(dto.getEvaluationGroupId());
            resultModelDetailReq.setGradeStatus(dto.getGradeStatus());
            resultModelDetailReq.setGradeGroupId(dto.getGradeGroupId());
            ResultModelItemReq itemReq = new ResultModelItemReq();
            BeanUtils.copyProperties(dto, itemReq);
            if(itemReq.getWeight().compareTo(BigDecimal.ZERO) == 0){//不限权重
                itemReq.setWeight(null);
            }
            if(dto.getMaxCount().intValue() == 10000){
                itemReq.setMaxCount(null);
            }
            List<ResultTagetLibItemReq> req = resultTagetLibService.selectResultTagetLibByModelReqId(dto.getId());
            itemReq.setTagetLibItems(req);
            modelItems.add(itemReq);
        }
        resultModelDetailReq.setModelItems(modelItems);
        return R.ok().put("data",resultModelDetailReq);
    }



     @RequestMapping("/getById")
     public R getById(@RequestBody ResultModel resultModel) {
        resultModel = resultModelService.selectResultModelById(resultModel.getId());
        return R.ok().put("data",resultModel);
     }


     @PostMapping("/update")
     public R update(@RequestBody @ApiParam ResultModel resultModel) {
         resultModelService.updateResultModelById(resultModel);
        return R.ok();
     }


     @PostMapping("/save")
     @ApiOperation("保存模板中的考核维度")
     public R save(@RequestBody @ApiParam ResultModel resultModel) {
        log.info("保存模板中的考核数据{}", resultModel);
        if(resultModel.getId() != null && resultModel.getId().intValue() > 0){
            return update(resultModel);
        }
        resultModelService.insertResultModel(resultModel);
        return R.ok().put("data",resultModel);
     }

    @PostMapping("/saveDetail")
    @ApiOperation("保存模板中的考核维度及指标")
    public R saveDetail(@RequestBody @ApiParam ResultModelDetailReq resultModelDetailReq) {
        log.info("保存指标的数据{}", resultModelDetailReq);
        return resultModelService.saveDetail(resultModelDetailReq);

    }


    @GetMapping("/delete")
    @ApiOperation("删除模板中的考核维度")
    public R delete(@RequestParam @ApiParam("维度id") Long id) {
        return resultModelService.deleteResultModelById(id);

     }
}
