package com.jpsoft.shinestar.modules.mobile.controller;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.base.dto.CompanySuperviseDTO;
import com.jpsoft.shinestar.modules.base.entity.*;
import com.jpsoft.shinestar.modules.base.service.*;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author 墨鱼_mo
 * @date 2021-1-20 15:07
 */
@RestController
@RequestMapping("/mobile/companySuperviseApi")
@Slf4j
public class CompanySuperviseApiController {

    @Autowired
    private CompanySuperviseService companySuperviseService;

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private PersonPopedomService personPopedomService;

    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private PersonSuperviseRelationService personSuperviseRelationService;


    @ApiOperation(value = "疫情专员统计数据")
    @PostMapping("companySuperviseAllData")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌", paramType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form"),
            @ApiImplicitParam(name = "date", value = "日期", required = true, paramType = "form"),
    })
    public MessageResult<Map> companySuperviseAllData(String token, @RequestAttribute String subject, String date) {
        MessageResult<Map> messageResult = new MessageResult<>();

        try {
            PersonInfo personInfo = personInfoService.get(new Long(subject));

            if (personInfo == null) {
                throw new Exception("人员信息不存在!");
            }

            PersonPopedom personPopedom = personPopedomService.get(personInfo.getPopedom());
            CompanyInfo companyInfo = companyInfoService.get(personPopedom.getCompanyId());


            Map<String, Object> map = new HashMap<>();

            //应测人数
            Integer totalNum = 0;
            //已测人数
            Integer totalCheckNum = 0;
            //异常人数
            Integer totalAbnormalNum = 0;
            //离荆人数
            Integer leaveNum = 0;

            List<PersonSuperviseRelation> personSuperviseRelationList = new ArrayList<>();
         //   List<PersonSuperviseRelation> personSuperviseRelationList = personSuperviseRelationService.findByPersonPopedomId(personInfo.getPopedom());
            if (companyInfo != null) {
                List<CompanyInfo> companyInfoList = companyInfoService.findAllCompanyByCode(companyInfo.getCode() + "%");
                for (CompanyInfo companyInfo1 : companyInfoList){
                    if ("1".equals(companyInfo1.getUnitType())){
                        PersonSuperviseRelation personSuperviseRelation = new PersonSuperviseRelation();
                        personSuperviseRelation.setCompanyId(companyInfo1.getId());
                        personSuperviseRelationList.add(personSuperviseRelation);
                    }

                }

            }


            if (personSuperviseRelationList.size() > 0) {
                totalNum = companySuperviseService.findTotalNumByCompanyIdAndDate(personSuperviseRelationList, date);
                totalCheckNum = companySuperviseService.findTotalCheckNumByCompanyIdAndDate(personSuperviseRelationList, date);
                totalAbnormalNum = companySuperviseService.findTotalAbnormalNumByCompanyIdAndDate(personSuperviseRelationList, date);
                leaveNum = companySuperviseService.findLeaveNumByCompanyIdAndDate(personSuperviseRelationList, date);

            }

            map.put("totalNum", totalNum==null?0:totalNum);
            map.put("totalCheckNum", totalCheckNum==null?0:totalCheckNum);
            map.put("totalAbnormalNum", totalAbnormalNum==null?0:totalAbnormalNum);
            map.put("leaveNum", leaveNum==null?0:leaveNum);
            messageResult.setData(map);
            messageResult.setResult(true);
            messageResult.setCode(200);

        } catch (Exception e) {
            log.error(e.getMessage(), e);

            messageResult.setResult(false);
            messageResult.setMessage(e.getMessage());
        }

        return messageResult;
    }

    @ApiOperation(value = "疫情专员列表数据")
    @PostMapping("companySuperviseListData")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌", paramType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form"),
            @ApiImplicitParam(name = "date", value = "日期", required = true, paramType = "form"),
    })
    public MessageResult<Map> commissionerListData(String token, @RequestAttribute String subject, String date, @RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        MessageResult<Map> messageResult = new MessageResult<>();

        try {
            PersonInfo personInfo = personInfoService.get(Long.valueOf(subject));

            if (personInfo == null) {
                throw new Exception("人员信息不存在!");
            }

            String recordTime = DateUtil.format(DateUtil.parse(date),"yyyy-MM-dd");
            List<PersonSuperviseRelation> personSuperviseRelationList = new ArrayList<>();
               //     List<PersonSuperviseRelation> personSuperviseRelationList = personSuperviseRelationService.findByPersonPopedomId(personInfo.getPopedom());

            PersonPopedom personPopedom = personPopedomService.get(personInfo.getPopedom());
            CompanyInfo companyInfo0 = companyInfoService.get(personPopedom.getCompanyId());
            if (companyInfo0 != null) {
                List<CompanyInfo> companyInfoList = companyInfoService.findAllCompanyByCode(companyInfo0.getCode() + "%");
                for (CompanyInfo companyInfo1 : companyInfoList){
                    if ("1".equals(companyInfo1.getUnitType())){
                        PersonSuperviseRelation personSuperviseRelation = new PersonSuperviseRelation();
                        personSuperviseRelation.setCompanyId(companyInfo1.getId());
                        personSuperviseRelationList.add(personSuperviseRelation);
                    }

                }
            }

            Page<CompanySupervise> page = new Page<>();
            List<CompanySuperviseDTO> list = new ArrayList<>();
            Map<String, Object> searchParams = new HashMap<>();
            if (personSuperviseRelationList.size() > 0) {
                searchParams.put("inCompanyId", personSuperviseRelationList);
                searchParams.put("recordTime",recordTime);
                List<Sort> sortList = new ArrayList<>();
                sortList.add(new Sort("test_rate", "asc"));
                page = companySuperviseService.pageSearch(searchParams, pageIndex, pageSize, true, sortList);

                List<CompanySupervise> companySuperviseList = page.getResult();
                Map<String, Object> map = new HashMap<>();
                if (companySuperviseList.size() > 0) {

                    for (CompanySupervise companySupervise : companySuperviseList) {

                        CompanySuperviseDTO companySuperviseDTO = new CompanySuperviseDTO();
                        CompanyInfo companyInfo = companyInfoService.get(companySupervise.getCompanyId());
                        BeanUtils.copyProperties(companySupervise, companySuperviseDTO);
                        companySuperviseDTO.setCompanyName(companyInfo.getName());

                        list.add(companySuperviseDTO);

                    }


                }

            }

            Map<String, Object> pageMap = new HashMap<>();

            pageMap.put("recordsTotal", page.getTotal());
            pageMap.put("recordsFiltered", page.getTotal());
            pageMap.put("totalPage", page.getPages());
            pageMap.put("pageNumber", page.getPageNum());
            pageMap.put("pageSize", page.getPageSize());
            pageMap.put("data", list);

            messageResult.setData(pageMap);
            messageResult.setResult(true);
            messageResult.setCode(200);

        } catch (Exception e) {
            log.error(e.getMessage(), e);

            messageResult.setResult(false);
            messageResult.setMessage(e.getMessage());
        }

        return messageResult;
    }

}
