package jnpf.cultivate.controller.web.identify;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Maps;
import jnpf.base.ActionResult;
import jnpf.base.entity.SuperBaseEntity;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.service.CultivateIdentifyTableService;
import jnpf.entity.cultivate.CultivateIdentifyTable;
import jnpf.model.cultivate.dto.identify.FtbIdentityOrgWisdomStatisticDTO;
import jnpf.model.cultivate.dto.identify.FtbIdentityPersonWisdomStatisticDTO;
import jnpf.model.cultivate.vo.identify.FtbCultivateIdentityOrgWisdomStatisticVO;
import jnpf.model.cultivate.vo.identify.FtbCultivateIdentityPersonWisdomStatisticVO;
import jnpf.util.EasyExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * web 实操鉴定统计
 * @Author: peng.hao
 */
@RestController
@RequestMapping(value = "/identify/statistic")
public class FtbCultivateIdentifyStatisticController {
    @Autowired
    CultivateIdentifyTableService  service;
    /**
     * 组织维度统计
     * @param statisticDTO
     * @return
     */
    @PostMapping("/org-list-statistics")
    public ActionResult<PageListVO<FtbCultivateIdentityOrgWisdomStatisticVO>> organizationListStatistics(
            @RequestBody FtbIdentityOrgWisdomStatisticDTO statisticDTO){
        PageListVO<FtbCultivateIdentityOrgWisdomStatisticVO> listVO = service.organizationListStatistics(statisticDTO);
        return ActionResult.success(listVO);
    }

    /**
     * 组织维度统计 导出
     * @param statisticDTO
     */
    @PostMapping("/org-list-statistics/export")
    public void exportInformationorganization(@RequestBody FtbIdentityOrgWisdomStatisticDTO statisticDTO, HttpServletResponse response) throws IOException {
        statisticDTO.setPageSize(999999);
        PageListVO<FtbCultivateIdentityOrgWisdomStatisticVO> listVO = service.organizationListStatistics(statisticDTO);
        EasyExcelUtil.simpleWrite(listVO.getList(), "组织维度统计", FtbCultivateIdentityOrgWisdomStatisticVO.class, response);
    }
    /**
     * 个人维度统计
     * @param personWisdomStatisticDTO
     * @return
     */
    @PostMapping("/person-list-statistics")
    public ActionResult<PageListVO<FtbCultivateIdentityPersonWisdomStatisticVO>> personListStatistics(
            @RequestBody FtbIdentityPersonWisdomStatisticDTO personWisdomStatisticDTO){
        PageListVO<FtbCultivateIdentityPersonWisdomStatisticVO> listVO = service.personListStatistics(personWisdomStatisticDTO);
        return ActionResult.success(listVO);
    }

    /**
     * 个人维度统计导出信息
     * @param personWisdomStatisticDTO
     */
    @PostMapping("/person-list-statistics/export")
    public void exportInformationPerson(@RequestBody FtbIdentityPersonWisdomStatisticDTO personWisdomStatisticDTO, HttpServletResponse response) throws IOException{
        PageListVO<FtbCultivateIdentityPersonWisdomStatisticVO> listVO = service.personListStatistics(personWisdomStatisticDTO);
        List<FtbCultivateIdentityPersonWisdomStatisticVO> list = listVO.getList();
        list.forEach(item->{
            if (StringUtils.isNotEmpty(item.getAuthenticationStatus())) {
                if (item.getAuthenticationStatus().equals("0")) {
                    item.setAuthenticationStatus("未鉴定");
                } else if (item.getAuthenticationStatus().equals("1")) {
                    item.setAuthenticationStatus("已鉴定");
                } else if (item.getAuthenticationStatus().equals("2")) {
                    item.setAuthenticationStatus("逾期未鉴定");
                }
            }
            if (StringUtils.isNotEmpty(item.getIdentificationResults())) {
                if (item.getIdentificationResults().equals("0")) {
                    item.setIdentificationResults("合格");
                } else if (item.getIdentificationResults().equals("1")) {
                    item.setIdentificationResults("优秀");
                } else if (item.getIdentificationResults().equals("2")) {
                    item.setIdentificationResults("不合格");
                }
            }
            if (StringUtils.isNotEmpty(item.getAuthenticationType())) {
                if (item.getAuthenticationType().equals("0")) {
                    item.setAuthenticationType("手动发起");
                } else if (item.getAuthenticationType().equals("1")) {
                    item.setAuthenticationType("课程学习鉴定");
                } else if (item.getAuthenticationType().equals("2")) {
                    item.setAuthenticationType("岗位学习鉴定");
                } else if (item.getAuthenticationType().equals("3")) {
                    item.setAuthenticationType("本人申请");
                }
            }
        });
        EasyExcelUtil.simpleWrite(list, "个人维度统计", FtbCultivateIdentityPersonWisdomStatisticVO.class, response);
    }
    /**
     * 查询列表
     * @return
     */
    @GetMapping("/query-correspond-list")
    public ActionResult<List<Map<String,Object>>> queryCorrespondingList(){
        List<CultivateIdentifyTable> list = service.lambdaQuery()
                .eq(SuperBaseEntity.SuperCUDBaseEntity::getDeleteMark, "0").list();
        List<Map<String,Object>> collect = list.stream().map(item -> {
            Map< String, Object> map = Maps.newHashMap();
            map.put("name",item.getName());
            map.put("id",item.getId());
            return map;
        }).collect(Collectors.toList());
        return ActionResult.success(collect);
    }


}
