package jnpf.cultivate.controller.web.certificate;

import com.google.common.collect.Maps;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.service.FtbCultivateCertificateService;
import jnpf.model.cultivate.dto.certificate.FtbCertificateOrgWisdomStatisticDTO;
import jnpf.model.cultivate.dto.certificate.FtbCertificatePersonWisdomStatisticDTO;
import jnpf.model.cultivate.po.certificate.FtbCertificateEntity;
import jnpf.model.cultivate.vo.certificate.FtbCertificateOrgWisdomStatisticVO;
import jnpf.model.cultivate.vo.certificate.FtbCertificatePersonWisdomStatisticVO;
import jnpf.util.EasyExcelUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
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("/certificate/statistic")
public class FtbCultivateCertificateStatisticController {

    @Resource
    private FtbCultivateCertificateService service;

    /**
     * 组织维度统计
     * @param statisticDTO
     * @return
     */
    @PostMapping("/org-list-statistics")
    public ActionResult<PageListVO<FtbCertificateOrgWisdomStatisticVO>> organizationListStatistics(
            @RequestBody FtbCertificateOrgWisdomStatisticDTO statisticDTO){
        PageListVO<FtbCertificateOrgWisdomStatisticVO> listVO = service.organizationListStatistics(statisticDTO);
        return ActionResult.success(listVO);
    }

    /**
     * 组织维度统计 导出
     * @param statisticDTO
     */
    @PostMapping("/org-list-statistics/export")
    public void exportInformationorganization(@RequestBody FtbCertificateOrgWisdomStatisticDTO statisticDTO, HttpServletResponse response) throws IOException {
        statisticDTO.setPageSize(-1);
        PageListVO<FtbCertificateOrgWisdomStatisticVO> listVO = service.organizationListStatistics(statisticDTO);
        List<FtbCertificateOrgWisdomStatisticVO> list = listVO.getList();
        list.forEach(item->{
            // 状态（0下架，1上架）
            if (item.getOnShelfStatus().equals("0")){
                item.setOnShelfStatus("下架");
            }else if (item.getOnShelfStatus().equals("1")){
                item.setOnShelfStatus("上架");
            }
        });
        EasyExcelUtil.simpleWrite(list, "组织维度统计", FtbCertificateOrgWisdomStatisticVO.class, response);
    }
    /**
     * 个人维度统计
     * @param personWisdomStatisticDTO
     * @return
     */
    @PostMapping("/person-list-statistics")
    public ActionResult<PageListVO<FtbCertificatePersonWisdomStatisticVO>> personListStatistics(
                                 @RequestBody FtbCertificatePersonWisdomStatisticDTO personWisdomStatisticDTO){
        PageListVO<FtbCertificatePersonWisdomStatisticVO> listVO = service.personalDimensionStatistics(personWisdomStatisticDTO);
        return ActionResult.success(listVO);
    }

    /**
     * 个人维度统计导出信息
     * @param personWisdomStatisticDTO
     */
    @PostMapping("/person-list-statistics/export")
    public void exportInformationPerson(@RequestBody FtbCertificatePersonWisdomStatisticDTO personWisdomStatisticDTO, HttpServletResponse response) throws IOException {
        personWisdomStatisticDTO.setPageSize(-1);
        PageListVO<FtbCertificatePersonWisdomStatisticVO> listVO = service.personalDimensionStatistics(personWisdomStatisticDTO);

        List<FtbCertificatePersonWisdomStatisticVO> list = listVO.getList();
        list.forEach(item->{
            if (item.getEffectiveStatus().equals("0")){
                item.setEffectiveStatus("生效中");
            }else if (item.getEffectiveStatus().equals("1")){
                item.setEffectiveStatus("已失效");
            }else if (item.getEffectiveStatus().equals("2")){
                item.setEffectiveStatus("已吊销");
            }
        });
        EasyExcelUtil.simpleWrite(list, "个人维度统计", FtbCertificatePersonWisdomStatisticVO.class, response);
    }
    /**
     * 查询证书列表
     * @return
     */
    @GetMapping("/query-correspond-list")
    public ActionResult<List<Map<String,Object>>> queryCorrespondingList(){
        List<FtbCertificateEntity> list = service.lambdaQuery().eq(FtbCertificateEntity::getEnabledMark,"1").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);
    }

}
