package com.lz.modules.performance.controller;


import com.alibaba.fastjson.JSONObject;
import com.lz.common.utils.PageUtils;
import com.lz.common.utils.R;
import com.lz.common.utils.StringUtil;
import com.lz.modules.app.dto.DepartmentsSimpleDto;
import com.lz.modules.app.dto.StaffSimpleDto;
import com.lz.modules.app.service.StaffService;
import com.lz.modules.flow.entity.EvaluationGroup;
import com.lz.modules.flow.entity.FlowManager;
import com.lz.modules.flow.entity.ResultModel;
import com.lz.modules.flow.model.EvaluationGroupDto;
import com.lz.modules.flow.req.EvaluationGroupReq;
import com.lz.modules.flow.service.EvaluationGroupService;
import com.lz.modules.flow.service.FlowManagerService;
import com.lz.modules.flow.service.ResultModelService;
import com.lz.modules.flow.service.StaffRoleDepartmentService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/evaluationGroup")
@Api(tags="考评组")
public class EvaluationGroupController {


    @Autowired
    private EvaluationGroupService evaluationGroupService;



    @Autowired
    private ResultModelService resultModelService;

    @Autowired
    private StaffService staffService;

    @Autowired
    private StaffRoleDepartmentService staffRoleDepartmentService;




    @GetMapping("/getById")
    @ApiOperation("获取考评组详情")
    @ApiResponses({@ApiResponse(code=200,message = "成功", response= EvaluationGroupDto.class)})
    public R getById(@RequestParam @ApiParam("考评组id") Long id) {
        EvaluationGroup evaluationGroup = evaluationGroupService.selectEvaluationGroupById(id);
        EvaluationGroupDto evaluationGroupDto = new EvaluationGroupDto();
        if(evaluationGroup.getDepIds() != null && evaluationGroup.getDepIds().length() > 0){

            List<String> ids = Arrays.stream(evaluationGroup.getDepIds().split(",")).map(new Function<String, String>() {
                @Override
                public String apply(String s) {
                    return s;
                }
            }).collect(Collectors.toList());
            List<DepartmentsSimpleDto> departmentsSimpleDtos = staffRoleDepartmentService.selectDepartmentSimpleInfos(ids);
            evaluationGroupDto.setDeps(departmentsSimpleDtos);
        }
        if(evaluationGroup.getStaffIds() != null && evaluationGroup.getStaffIds().length() > 0){
            List<Long> ids = Arrays.stream(evaluationGroup.getStaffIds().split(",")).map(new Function<String, Long>() {
                @Override
                public Long apply(String s) {
                    return Long.parseLong(s);
                }
            }).collect(Collectors.toList());

            //指定人员，搜索人员信息
            List<StaffSimpleDto> staffSimpleDtos = staffService.selectStaffSimpleInfoByIds(ids);
            evaluationGroupDto.setStaffs(staffSimpleDtos);
        }

        if(evaluationGroup.getManagerIds() != null && evaluationGroup.getManagerIds().length() > 0){
            List<Long> ids = Arrays.stream(evaluationGroup.getManagerIds().split(",")).map(new Function<String, Long>() {
                @Override
                public Long apply(String s) {
                    return Long.parseLong(s);
                }
            }).collect(Collectors.toList());

            //指定人员，搜索人员信息
            List<StaffSimpleDto> staffSimpleDtos = staffService.selectStaffSimpleInfoByIds(ids);
            evaluationGroupDto.setManagers(staffSimpleDtos);
        }
        if(evaluationGroup.getOutIds() != null && evaluationGroup.getOutIds().length() > 0){
            List<Long> ids = Arrays.stream(evaluationGroup.getOutIds().split(",")).map(new Function<String, Long>() {
                @Override
                public Long apply(String s) {
                    return Long.parseLong(s);
                }
            }).collect(Collectors.toList());

            //指定人员，搜索人员信息
            List<StaffSimpleDto> staffSimpleDtos = staffService.selectStaffSimpleInfoByIds(ids);
            evaluationGroupDto.setOuts(staffSimpleDtos);
        }

        BeanUtils.copyProperties(evaluationGroup, evaluationGroupDto);
        return R.ok().put("data",evaluationGroupDto);
    }


     @PostMapping("/getGroups")
     @ApiOperation("获取考评组列表")
     @ApiResponses({@ApiResponse(code=200,message = "成功", response=EvaluationGroup.class)})
     public R getGroups(@RequestBody @ApiParam EvaluationGroupReq req) {
         PageUtils pageUtils = evaluationGroupService.selectEvaluationGroupByReq(req);
        return R.ok().put("data",pageUtils);
     }


     @PostMapping("/update")
     public R update(@RequestBody @ApiParam EvaluationGroup evaluationGroup) {
         evaluationGroupService.updateEvaluationGroupById(evaluationGroup);
        return R.ok();
     }


     @PostMapping("/save")
     @ApiOperation("保存考评组")
     @ApiResponses({@ApiResponse(code = 200, message = "成功", response = EvaluationGroup.class)})
     public R save(@RequestBody @ApiParam EvaluationGroup evaluationGroup) {
        if(evaluationGroup.getId() != null && evaluationGroup.getId().intValue() > 0){
            EvaluationGroup evaluationGroup1 = evaluationGroupService.selectEvaluationGroupByName(evaluationGroup.getName());
            if(evaluationGroup1 == null || evaluationGroup1.getId().equals(evaluationGroup.getId())){
                evaluationGroupService.updateEvaluationGroupById(evaluationGroup);
            }else {
                return R.error("已经存在相同名称考核组");
            }

        }else{
            EvaluationGroup evaluationGroup1 = evaluationGroupService.selectEvaluationGroupByName(evaluationGroup.getName());
            if(evaluationGroup1 != null){
                return R.error("已经存在相同名称考核组");
            }
            evaluationGroupService.insertEvaluationGroup(evaluationGroup);
        }

        return R.ok().put("data", evaluationGroup);
     }


    @GetMapping("/delete")
    @ApiOperation("删除考评组")
    public R delete(@RequestParam @ApiParam("组id") Long id) {
        resultModelService.deleteResultModelByGroupId(id);
        return evaluationGroupService.deleteEvaluationGroupById(id);
        //return R.ok();
     }
}
