package com.zd.laboratory.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.zd.common.core.domain.R;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.security.service.TokenService;
import com.zd.common.swagger.config.Knife4jConfiguration;
import com.zd.laboratory.domain.AdminWranCount;
import com.zd.laboratory.domain.LabSubject;
import com.zd.laboratory.domain.SubDiyVO;
import com.zd.laboratory.domain.dto.LabSubListQuery;
import com.zd.laboratory.domain.vo.LabRiskPlanAbnormalLogVO;
import com.zd.laboratory.service.ILabRiskPlanAbnormalLogService;
import com.zd.laboratory.service.ILabSubjectService;
import com.zd.laboratory.service.impl.SubQueryManager;
import com.zd.system.api.domain.SubQueryConfig;
import com.zd.system.api.domain.SysDept;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * 实验室控制管理
 */
@Api(tags = {"实验室控制管理-V2"})
@ApiSupport(author = Knife4jConfiguration.Author.ZP)
@RestController
@RequestMapping("/subject/manger/V2")
public class LabSubMangerV2Controller extends BaseController {

    @Autowired
    SubQueryManager subQueryManager;
    @Autowired
    private TokenService tokenService;

    @Autowired
    ILabSubjectService subjectService;

    @Autowired
    private ILabRiskPlanAbnormalLogService labRiskPlanAbnormalLogService;

    /**
     * 查询实验室控制数据列表- 数据权限（包括负责人） 小程序我的实验室
     */
    @ApiOperation("查询实验室控制数据列表- 数据权限（包括负责人）")
    @PostMapping("/list")
    public ResultData<List<SubDiyVO>> querySubCListVO(@RequestBody LabSubListQuery labSubListQuery) {
        LabSubject subject = new LabSubject();
        BeanUtils.copyProperties(labSubListQuery, subject);
        if (labSubListQuery.getLevel() != null) {
            subject.setLevel(Long.parseLong(labSubListQuery.getLevel() + ""));
        }
        List<SubDiyVO> subDiyVOList = subQueryManager.querySubs(subject, SubQueryConfig.subQueryConfigList);
        return ResultData.success(subDiyVOList);
    }

    @PostMapping("/list/ids")
    public R<List<LabSubject>> listByIds(@RequestBody List<Long> ids) {
        List<LabSubject> list = subjectService.getByIds(ids);
        return R.ok(list);
    }

    @GetMapping("/list/name")
    public R<List<LabSubject>> listByName(@RequestParam("name") String name) {
        LabSubject subject= new LabSubject();
        subject.setSearchValue(name);
        List<LabSubject> list = subjectService.selectLabSubjectList(subject);
        return R.ok(list);
    }

    /**
     * 查询实验室数据 通过ID 和查询配置
     */
    @ApiOperation("查询实验室数据 通过ID 和配置")
    @PostMapping("/diy/{id}/common")
    public ResultData<List<SubDiyVO>> querySubCListVO(@PathVariable("id") Long id, @RequestBody SubQueryConfig subQueryConfig) {
        List<SubDiyVO> subDiyVOList = subQueryManager.querySubs(subQueryConfig, id);
        return ResultData.success(subDiyVOList);
    }

    /**
     * 查询我管理的实验室及当前预警数量
     */
    @ApiOperation("查询我管理的实验室及当前预警数量")
    @GetMapping("/admin/subandwran/count")
    public ResultData<AdminWranCount> querySubandwranCount() {
        LabSubject subject = new LabSubject();
        AdminWranCount adminWranCount = new AdminWranCount();
        //实验室数量 数据权限+业务权限
        int i = 0;
        try {
            SysDept dept = tokenService.getLoginUser().getSysUser().getDept();
            // 只查询所属部门的实验室，没有部门则无数据
            if (dept.getParentId() == 100) {
                subject.setDeptId(dept.getDeptId());
                i = subjectService.myAdminSubCount(subject);
            } else if (dept.getParentId() == 0) {
                i = subjectService.myAdminSubCount(subject);
            }
        } catch (Exception e) {
            e.fillInStackTrace();
        }

        adminWranCount.setAdminSubCount(i);
        // 预案执行记录 需要老柴提供
        LabRiskPlanAbnormalLogVO labRiskPlanAbnormalLogVo = new LabRiskPlanAbnormalLogVO();
        Map<String, Integer> riskPlanMap = labRiskPlanAbnormalLogService.selectLabRiskPlanAppCount(labRiskPlanAbnormalLogVo);
        String allday = riskPlanMap.get("allday") + "";
        adminWranCount.setWranDoCount(Integer.parseInt(allday));
        return ResultData.success(adminWranCount);
    }
}
