package com.zd.laboratory.controller;

import com.zd.common.core.domain.R;
import com.zd.common.core.web.controller.BaseController;
import com.zd.laboratory.service.ILabGradeManageWorkService;
import com.zd.system.api.laboratory.domain.LabGradeManageRecord;
import com.zd.system.api.laboratory.domain.vo.LabGradeManageWorkVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分级管控工作Controller
 *
 * @author liubo
 * @date 2022-02-10
 */
@RestController
@Api(tags = "【分级管控工作】")
@RequestMapping("/gradeManageWork")
public class LabGradeWorkController extends BaseController {
    @Autowired
    private ILabGradeManageWorkService labGradeManageWorkService;

    /**
     * 查询分级管控工作列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询分级管控工作列表")
    public R<List<LabGradeManageWorkVO>> list(LabGradeManageWorkVO labGradeManageWork) {
        List<LabGradeManageWorkVO> list = labGradeManageWorkService.selectMyGradeManageWorkListV2(labGradeManageWork);
        return R.ok(list);
    }

    /**
     * 获取分级管控工作详细信息
     * -- subId 实验室ID
     * @param id
     */
    @ApiOperation(value = "获取分级管控工作详细信息")
    @GetMapping(value = "/{id}")
    public R<LabGradeManageRecord> getInfo(@PathVariable("id") Long id) {
        return R.ok(labGradeManageWorkService.selectLabGradeManageWorkById(id));
    }

    /**
     * 管控消息执行通知 - 处理工作信息查询
     */
    @ApiOperation(value = "管控消息执行通知 - 处理工作信息查询")
    @PostMapping(value = "/queryByMessage")
    public R<LabGradeManageWorkVO> queryByMessage(@RequestBody LabGradeManageRecord labGradeManageRecord) {
        return R.ok(labGradeManageWorkService.queryByMessage(labGradeManageRecord));
    }

    /**
     * 完成分级管控工作 ruleId
     * @return
     */
    @ApiOperation(value = "完成分级管控工作")
    @PostMapping(value = "/finish")
    public R<LabGradeManageRecord> finish(@RequestBody LabGradeManageRecord labGradeManageRecord) {
        if(labGradeManageRecord.getSubId() == null){
            return R.fail("参数异常：subId 不能为空");
        }
        if(labGradeManageRecord.getRuleId() == null && labGradeManageRecord.getRuleUserId() == null){
            return R.fail("参数异常：ruleId 不能为空");
        }
        if(labGradeManageRecord.getDetail() == null && StringUtils.isBlank(labGradeManageRecord.getDetail().getRemark())){
            return R.fail("确认内容不能为空");
        }
        return R.ok(labGradeManageWorkService.finishMyLabGradeManageWorkById(labGradeManageRecord));
    }


    /**
     * 查询分级管控未完成工作列表
     */
    @PostMapping("/havingList")
    @ApiOperation(value = "查询分级管控工作列表")
    public R<List<LabGradeManageWorkVO>> havingList(@RequestBody LabGradeManageWorkVO labGradeManageWork) {
        List<LabGradeManageWorkVO> list = labGradeManageWorkService.selectMyGradeManageWorkListV2(labGradeManageWork);
        // 去除已经完成的部分
        /*for (int j = 0; j < list.size(); j++) {

            List<LabGradeManageWorkDetailVO> details = list.get(j).getDetailList();
            if(CollectionUtils.isNotEmpty(details)){
                for (int i = 0; i < details.size(); i++) {
                    if(details.get(i).getStatus() == 2){
                        details.remove(i);
                        i--;
                    }
                }
                if(details.size() == 0){
                    list.remove(j);
                    j --;
                }

            }
        }*/

        int count = 0;
        for (LabGradeManageWorkVO labGradeManageWorkVO : list) {
            count += labGradeManageWorkVO.getDetailList() != null ? labGradeManageWorkVO.getDetailList().size() : 0;
        }

        return R.ok(list, count);
    }


    /**
     * 查询分级管控已完成工作列表
     */
    @PostMapping("/finishList")
    @ApiOperation(value = "查询分级管控工作列表")
    public R<List<LabGradeManageWorkVO>> finishList(@RequestBody LabGradeManageRecord labGradeManageRecord) {
        List<LabGradeManageWorkVO> list = labGradeManageWorkService.finishList(labGradeManageRecord);
        return R.ok(list);
    }
}
