package com.zjrs.zwnw.sxgl.blo.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.mohrss.leaf.core.framework.domain.blo.impl.BLOImpl;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.persistens.PageUtil;
import com.zjrs.zwnw.sxgl.blo.SxtjBLO;
import com.zjrs.zwnw.sxgl.dto.SxtjListDTO;
import com.zjrs.zwnw.sxgl.dto.SxtjQueryDTO;
import com.zjrs.zwnw.sxgl.entity.SxpzxxMapper;
import com.zjrs.zwnw.sxgl.entity.SxtjMapper;
import com.zjrs.zwnw.sxgl.po.SxpzxxPO;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SxtjBLOimpl extends BLOImpl implements SxtjBLO {
    @Resource
    SxpzxxMapper sxpzxxMapper;
    @Resource
    SxtjMapper sxtjMapper;

    @Override
    public Map<String, Object> getSxtj(String ssbmmc) {
        Map<String, Object> resultMap = new HashMap<>();
        QueryWrapper<SxpzxxPO> queryWrapper = new QueryWrapper<SxpzxxPO>();
        queryWrapper.select("COUNT(1) sxzl,\n" +
                "       COUNT(CASE WHEN slqd like '%1%' THEN 1 ELSE null END) gtjb,\n" +
                "       COUNT(CASE WHEN slqd like '%2%' THEN 1 ELSE null END) ggfw,\n" +
                "       COUNT(CASE WHEN slqd like '%3%' THEN 1 ELSE null END) wxxcx,\n" +
                "       COUNT(CASE WHEN slqd like '%4%' THEN 1 ELSE null END) zzytj,\n" +
                "       COUNT(CASE WHEN slqd = '5' THEN 1 ELSE null END) bwb,\n" +
                "       COUNT(CASE WHEN slqd = '6' THEN 1 ELSE null END) qwb,\n" +
                "       COUNT(CASE WHEN dxccs = '0' THEN 1 ELSE null END) pt0,\n" +
                "       COUNT(CASE WHEN dxccs = '1' THEN 1 ELSE null END) pt1,\n" +
                "       COUNT(CASE WHEN dxccs = '2' THEN 1 ELSE null END) pt2 ");
        if(StringUtils.isNotBlank(ssbmmc)){
            queryWrapper.eq("ssbmmc", ssbmmc);
        }
        List<Map<String, Object>> tjList = this.sxpzxxMapper.selectMaps(queryWrapper);
        resultMap = tjList.get(0);
        return resultMap;
    }

    @Override
    public PageResult querySxtjList(SxtjQueryDTO dto, int page, int rows) {
        Page<SxtjListDTO> page1 = new Page<>(page, rows);

        Date t = new Date();
        SimpleDateFormat yearDf = new SimpleDateFormat("yyyy");//本年
        SimpleDateFormat monthDf = new SimpleDateFormat("yyyyMM");//本月
        dto.setBn(yearDf.format(t));
        
        Page<SxtjListDTO> pageResult = (Page) this.sxtjMapper.querySxtjList(page1, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }
}
