package org.springblade.manager.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.mixsmart.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.business.entity.AssessmentUser;
import org.springblade.business.feign.OpinionUserClient;
import org.springblade.business.vo.AssessmentUserContentVO;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.manager.service.SaveUserInfoByProjectService;
import org.springblade.system.entity.DictBiz;
import org.springblade.system.feign.IDictBizClient;
import org.springblade.system.user.entity.User;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 考核区接口
 */
@RestController
@AllArgsConstructor
@RequestMapping("/assessmentUser")
@Api(value = "考核区接口", tags = "考核区接口")
public class AssessmentUserController extends BladeController {

    private final OpinionUserClient opinionUserClient;

    private final SaveUserInfoByProjectService saveUserInfoByProjectService;

    private final IDictBizClient dictBizClient;

    /**
     * 查询所有考核人员的考核记录
     */
    @GetMapping("/queryAllUserAssessmentContent")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "查询所有考核人员的考核记录")
    public R<List<AssessmentUserContentVO>> queryAllUserAssessmentContent(){
        List<AssessmentUserContentVO> result = new ArrayList<>();
        //获取考核分类
        List<DictBiz> dictBizs = this.dictBizClient.getList("assessment_type", "notRoot").getData();
        //获取当前系统下所有维护人员
        List<User> assessmentUserList = this.saveUserInfoByProjectService.findMaintenanceUser();
        assessmentUserList.forEach(user -> {
            AssessmentUserContentVO vo = new AssessmentUserContentVO();
            vo.setUserName(user.getRealName());

            //查询当前用户的考核记录
            List<AssessmentUser> assessmentUsers = this.opinionUserClient.queryUserAssessmentByUserId(user.getId().toString());
            if(assessmentUsers != null && assessmentUsers.size() > 0){
                for(DictBiz dictBiz : dictBizs){
                    //考核金额
                    BigDecimal assessmentMoney = new BigDecimal("0.00");
                    int count = 0;
                    Iterator<AssessmentUser> iterator = assessmentUsers.iterator();
                    while (iterator.hasNext()){
                        AssessmentUser assessmentUser = iterator.next();
                        if(dictBiz.getId().toString().equals(assessmentUser.getAssessmentType())){
                            //同类型，统计
                            assessmentMoney = assessmentMoney.add(new BigDecimal(assessmentUser.getAssessmentMoney()));
                            count++;
                            iterator.remove();
                        }
                    }
                    //设置参数
                    vo.setAssessmentList(dictBiz.getDictKey(), assessmentMoney.toString(), count);
                }
            }
            //设置返回结果
            result.add(vo);
        });
        return R.data(result);
    }

    /**
     * 查询考核类型字典
     */
    @GetMapping("/queryAssessmentType")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "查询考核类型字典")
    public R<List<DictBiz>> queryAssessmentType(){
        return this.dictBizClient.getList("assessment_type", "notRoot");
    }

    /**
     * 获取当前系统下所有维护人员
     */
    @GetMapping("/findMaintenanceUser")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "获取当前系统下所有维护人员")
    public R<List<User>> findMaintenanceUser(){
        return R.data(this.saveUserInfoByProjectService.findMaintenanceUser());
    }

    /**
     * 取消考核
     */
    @PostMapping("/cancelAssessment")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "取消考核")
    @ApiImplicitParam(name = "id", value = "分页中的assessmentId", required = true)
    public R<Boolean> cancelAssessment(@RequestParam String id){
        if("-1".equals(id) || StringUtils.isEmpty(id)){
            return R.data(-1, false, "缺少id或id不能为-1");
        }
        return R.data(this.opinionUserClient.cancelAssessment(id));
    }

    /**
     * 详情查看
     */
    @PostMapping("/getById")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "详情查看")
    @ApiImplicitParam(name = "id", value = "分页中的assessmentId", required = true)
    public R<AssessmentUser> getById(@RequestParam String id){
        return R.data(this.opinionUserClient.getById(id));
    }

    /**
     * 修改考核
     */
    @PostMapping("/updateUserAssessment")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "修改考核")
    public R<Boolean> updateUserAssessment(@RequestBody AssessmentUser assessmentUser){
        BladeUser user = AuthUtil.getUser();
        assessmentUser.setUpdateUser(user.getUserId());
        assessmentUser.setUpdateTime(new Date());

        return R.data(this.opinionUserClient.updateUserAssessment(assessmentUser));
    }

    /**
     * 新增考核
     */
    @PostMapping("/saveUserAssessment")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "新增考核")
    public R<Boolean> saveUserAssessment(@RequestBody AssessmentUser assessmentUser){
        BladeUser user = AuthUtil.getUser();
        //设置创建人
        assessmentUser.setCreateUser(user.getUserId());
        assessmentUser.setCreateDept(user.getDeptId().contains(",") ? Long.parseLong(user.getDeptId().split(",")[0]) : Long.parseLong(user.getDeptId()));
        assessmentUser.setCreateTime(new Date());
        return R.data(this.opinionUserClient.saveUserAssessment(assessmentUser));
    }

//    /**
//     * 考核区分页
//     */
//    @GetMapping("/queryUserAssessmentPage")
//    @ApiOperationSupport(order = 1)
//    @ApiOperation(value = "考核区分页")
//    public R<IPage<AssessmentUserVOS>> queryUserAssessmentPageList(AssessmentUserVOS vo){
//        Query query = new Query();
//        query.setCurrent(vo.getCurrent());
//        query.setSize(vo.getSize());
//        //分页信息
//        IPage<AssessmentUserVOS> iPage = Condition.getPage(query);
//        //总数
//        Integer count = this.opinionUserClient.countUserAssessmentPageList(vo);
//        //分页数据
//        List<AssessmentUserVOS> result = this.opinionUserClient.queryUserAssessmentPageList(vo);
//
//        iPage.setTotal(count);
//        iPage.setRecords(result);
//
//        return R.data(iPage);
//    }

}
