package thyyxxk.webserver.service.medicalinsurance;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.constants.sidicts.MedType;
import thyyxxk.webserver.constants.sidicts.PsnType;
import thyyxxk.webserver.dao.his.medicalinsurance.TUploadDrgGroupDao;
import thyyxxk.webserver.entity.HeadInfo;
import thyyxxk.webserver.entity.querydata.TUploadDrgGroup;
import thyyxxk.webserver.service.hutoolcache.RegionCache;
import thyyxxk.webserver.utils.ExcelUtil;
import thyyxxk.webserver.utils.StringUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lihong
 * @since 2023-01-29
 */
@Service
public class TUploadDrgGroupService extends ServiceImpl<TUploadDrgGroupDao, TUploadDrgGroup> {

    @Resource
    private RegionCache regionCache;

    public IPage<TUploadDrgGroup> queryUploadDrgGroupPage(TUploadDrgGroup uploadDrgGroup){
        IPage<TUploadDrgGroup> page = new Page(uploadDrgGroup.getCurrentPage(),uploadDrgGroup.getPageSize());
        QueryWrapper<TUploadDrgGroup> query = gettUploadDrgGroupQueryWrapper(uploadDrgGroup);
        baseMapper.listPage(page,query);
        for(TUploadDrgGroup item : page.getRecords()){
            item.setMedTypeName(MedType.getName(item.getMedType()));
            if(StrUtil.isBlank(item.getClrOptinsName())){
                if("439900".equals(item.getClrOptins())){
                    item.setClrOptinsName("省本级");
                }else if("430199".equals(item.getClrOptins())){
                    item.setClrOptinsName("市本级");
                }
            }
            item.setInsuplcAdmdvsName(regionCache.get(item.getInsuplcAdmdvs()));
            item.setPsnTypeName(PsnType.get(item.getPsnType()) == null? "" : PsnType.get(item.getPsnType()).getName());
        }
        return page;
    }

    @NotNull
    private QueryWrapper<TUploadDrgGroup> gettUploadDrgGroupQueryWrapper(TUploadDrgGroup uploadDrgGroup) {
        QueryWrapper<TUploadDrgGroup> query = new QueryWrapper<>();
        query.eq("1",1);
        query.eq(StringUtil.notBlank(uploadDrgGroup.getPatNo()),"t1.pat_no", uploadDrgGroup.getPatNo());
        query.eq(uploadDrgGroup.getTimes() !=null,"t1.times", uploadDrgGroup.getTimes());
        query.eq(uploadDrgGroup.getLedgerSn() !=null,"t1.ledger_sn", uploadDrgGroup.getLedgerSn());
        query.ge(StringUtil.notBlank(uploadDrgGroup.getSetlStartTime()), "t1.setl_time", uploadDrgGroup.getSetlStartTime());
        query.le(StringUtil.notBlank(uploadDrgGroup.getSetlEndTime()), "t1.setl_time", uploadDrgGroup.getSetlEndTime());
        query.likeRight(uploadDrgGroup.getBlType() != null && uploadDrgGroup.getBlType() != 0 &&  uploadDrgGroup.getBlType() !=11,"t1.bl", TUploadDrgGroup.BlTypeEnum.getNameByCode(uploadDrgGroup.getBlType()));
        query.likeLeft(uploadDrgGroup.getBlType() != null && uploadDrgGroup.getBlType()==11 ,"t1.code","QY" );
        return query;
    }


    public void delAndSaveData(TUploadDrgGroup uploadDrgGroup) {
        QueryWrapper<TUploadDrgGroup> query = new QueryWrapper<>();
        query.eq("pat_no",uploadDrgGroup.getPatNo());
        query.eq("times", uploadDrgGroup.getTimes());
        query.eq("ledger_sn",uploadDrgGroup.getLedgerSn());
        baseMapper.delete(query);
        baseMapper.insert(uploadDrgGroup);
    }
    /**
     * @description:导出
     * @author: lihong
     * @date: 2023/1/30 9:23
     * @param: response
     * @param: query
     **/
    public void exportDrgGroupInfo(HttpServletResponse response, TUploadDrgGroup query) {
        query.setPageSize(100000);
        query.setCurrentPage(1);
        IPage<TUploadDrgGroup> tUploadDrgGroupIPage = queryUploadDrgGroupPage(query);
        List<Map<String, Object>> result = ExcelUtil.beanToMap(tUploadDrgGroupIPage.getRecords(),TUploadDrgGroup.class);
        String[] display = {"住院号","住院次数","账页号","出院科室","分组编码","分组名称","权重","病历类型","盈亏额","标杆费用","总费用","结算时间","医疗类别","参保地","清算机构","人员类别","分组提示"};
        String[] names = {"patNo","times","ledgerSn","outDeptName","code","name","weight","bl","profit","feeStand","totalFee","setlTime","medTypeName","insuplcAdmdvsName","clrOptinsName","psnTypeName","groupMessages"};
        List<HeadInfo> headList = ExcelUtil.createHeadInfos(display,names);
        ExcelUtil.exportExcelReport(response,result,headList,"drg分组信息");
    }
}
