package com.tofly.feesapi.yhgl.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.feesapi.bjgl.entity.YhCbkxx;
import com.tofly.feesapi.bjgl.entity.vo.YhCbkxxSelect;
import com.tofly.feesapi.bjgl.service.YhCbkxxService;
import com.tofly.feesapi.bmgl.entity.BmCbb;
import com.tofly.feesapi.bmgl.entity.BmCbpq;
import com.tofly.feesapi.bmgl.entity.BmSj;
import com.tofly.feesapi.bmgl.service.BmCbbService;
import com.tofly.feesapi.bmgl.service.BmCbpqService;
import com.tofly.feesapi.bmgl.service.BmSjService;
import com.tofly.feesapi.cbgl.entity.YyCblr;
import com.tofly.feesapi.cbgl.service.IZnbCbLogService;
import com.tofly.feesapi.cbgl.service.YyCblrService;
import com.tofly.feesapi.common.NumberHelper;
import com.tofly.feesapi.common.constant.CbbzEnum;
import com.tofly.feesapi.common.constant.JsbzEnum;
import com.tofly.feesapi.common.constant.ShbzEnum;
import com.tofly.feesapi.common.constant.YhztEnum;
import com.tofly.feesapi.common.core.ResultRespone;
import com.tofly.feesapi.common.core.dto.Order;
import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.common.enums.PljdcwxxEnum;
import com.tofly.feesapi.common.enums.YhDxhEnum;
import com.tofly.feesapi.common.exception.FeesErrorCode;
import com.tofly.feesapi.common.exception.FeesException;
import com.tofly.feesapi.common.util.GuidGenerator;
import com.tofly.feesapi.common.util.SysConstant;
import com.tofly.feesapi.common.util.ToolsUtils;
import com.tofly.feesapi.mxcx.entity.dto.YhwjFilter;
import com.tofly.feesapi.mxcx.entity.vo.YhNdjtslSelect;

import com.tofly.feesapi.mxcx.entity.vo.YhwjSelect;
import com.tofly.feesapi.security.util.SecurityUtils;
import com.tofly.feesapi.udb.entity.Company;
import com.tofly.feesapi.udb.service.ICompanyService;
import com.tofly.feesapi.yhgl.entity.YhKhjbxx;
import com.tofly.feesapi.yhgl.entity.YhKhjbxxLog;
import com.tofly.feesapi.yhgl.entity.YhkhjbxxExcel;
import com.tofly.feesapi.yhgl.entity.dto.*;
import com.tofly.feesapi.yhgl.entity.vo.*;
import com.tofly.feesapi.yhgl.mapper.YhKhjbxxMapper;
import com.tofly.feesapi.yhgl.service.YhKhjbxxLogService;
import com.tofly.feesapi.yhgl.service.YhKhjbxxService;
import com.tofly.feesapi.znbgl.entity.dto.ZnbYhdaFilter;
import com.tofly.feesapi.znbgl.entity.vo.ZnbYhdaSelect;
import com.tofly.feesapi.znbgl.service.ZnbDaLogService;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户档案
 */
@Service("yhKhjbxxService")
public class YhKhjbxxServiceImpl extends ServiceImpl<YhKhjbxxMapper, YhKhjbxx> implements YhKhjbxxService {
    @Autowired
    private YhCbkxxService yhCbkxxService;
    @Autowired
    private YhKhjbxxLogService yhKhjbxxLogService;
    @Autowired
    private BmCbpqService cbpqService;
    @Autowired
    private BmCbbService cbbService;
    @Autowired
    private IZnbCbLogService znbCbLogService;
    @Autowired
    private YyCblrService yyCblrService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private ZnbDaLogService znbDaLogService;
    @Autowired
    private BmSjService bmSjService;
    @Autowired
    private ToolsUtils toolsUtils;
    @Override
    public PageResult<YhkhjbxxSelect> getPage(PageDto pageDto, YhkhjbxxFilter filter) {
        List<Order> orders = pageDto.getOrders();
        orders = orders.stream().map(item -> {
            if (item.getColumn().equals("yhbh")) {
                Order order = new Order();
                order.setColumn("a.yhbh");
                order.setAsc(item.isAsc());
                return order;
            }
            return item;
        }).collect(Collectors.toList());
        pageDto.setOrders(orders);
        PageDto<YhkhjbxxSelect> page = this.baseMapper.getPage(pageDto, filter);
        return page.toVo();
    }

    @Override
    public PageResult<YhkhjbxxFfgxzSelect> getFfgxPage(PageDto pageDto, YhkhjbxxFilter filter) {
        List<Order> orders = pageDto.getOrders();
        orders = orders.stream().map(item -> {
            if (item.getColumn().equals("yhbh")) {
                Order order = new Order();
                order.setColumn("a.yhbh");
                order.setAsc(item.isAsc());
                return order;
            }
            return item;
        }).collect(Collectors.toList());
        pageDto.setOrders(orders);
        PageDto<YhkhjbxxFfgxzSelect> page = this.baseMapper.getFfgxPage(pageDto, filter);
        return page.toVo();
    }

    @Override
    public PageResult<ZnbYhdaSelect> getZnbDaPage(PageDto pageDto, ZnbYhdaFilter query) {
        List<Order> orders = pageDto.getOrders();
        orders = orders.stream().map(item -> {
            if (item.getColumn().equals("yhbh")) {
                Order order = new Order();
                order.setColumn("a.yhbh");
                order.setAsc(item.isAsc());
                return order;
            }
            return item;
        }).collect(Collectors.toList());
        pageDto.setOrders(orders);
        PageDto<ZnbYhdaSelect> page = this.baseMapper.getZnbDaPage(pageDto, query);
        return page.toVo();
    }

    @Override
    public List<YhkhjbxxSelect2> getList(YhkhjbxxFilter filter) {
        if (StringUtil.isEmpty(filter.getUserId())) {
            filter.setUserId(SecurityUtils.getUserId().toString());
        }
        List<YhkhjbxxSelect2> list = this.baseMapper.getList(filter);
        return list;
    }

    @Override
    public String getYhbh(String cbbbm) {
        YhKhjbxx entity = this.lambdaQuery().select(YhKhjbxx::getYhbh)
                .eq(YhKhjbxx::getSscbb, cbbbm)
                .last("AND rownum = 1 ORDER BY yhbh DESC")
                .one();
        String yhbh = "";
        String format = "%05d%03d";
        if (entity != null) {
            String yhbh1 = entity.getYhbh();
            int curr = Integer.valueOf(yhbh1.substring(5));
            yhbh = String.format(format, Integer.valueOf(cbbbm), curr + 1);
        } else {
            yhbh = String.format(format, Integer.valueOf(cbbbm), 1);
        }
        return yhbh;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(YhKhjbxxInsert insert) {
        YhKhjbxx yhKhjbxx = new YhKhjbxx();
        BeanUtils.copyProperties(insert, yhKhjbxx);
        String sscbpq = insert.getSscbpq();
        if (StringUtils.isEmpty(yhKhjbxx.getSscbpq())) {
            throw new FeesException("片区不能为空", FeesErrorCode.BUSINESS_ERROR);
        }
        String yhbh = this.getYhbh(sscbpq);
        yhKhjbxx.setYhbh(yhbh);
        check(yhKhjbxx, true);
        this.save(yhKhjbxx);//添加用户档案

        yhCbkxxService.save(insert, yhbh);//添加水表档案
        saveLog(yhKhjbxx, null, "添加");//添加用户档案操作历史
        return true;
    }

    @Override
    public boolean update(YhKhjbxxUpdate update) {
        String yhbh = update.getYhbh();
        if (StringUtils.isBlank(yhbh)) {
            throw new FeesException("用户编号不能为空", FeesErrorCode.BUSINESS_ERROR);
        }

        YhKhjbxx oldYhKhjbxx = this.getById(yhbh);
        if (oldYhKhjbxx == null) {
            throw new FeesException("用户不存在", FeesErrorCode.BUSINESS_ERROR);
        }

        YhKhjbxx yhKhjbxx = new YhKhjbxx();
        BeanUtils.copyProperties(oldYhKhjbxx, yhKhjbxx);
        String[] ignoreProperties = NumberHelper.getNullPropertyNames(update);
        BeanUtils.copyProperties(update, yhKhjbxx, ignoreProperties);
        check(yhKhjbxx, false);
        this.updateById(yhKhjbxx);//更新用户档案
        saveLog(yhKhjbxx, oldYhKhjbxx, "修改");//添加用户档案操作历史
        return true;
    }

    @Override
    public YhKhjbxxView getViewById(String yhbh) {
        YhKhjbxxView view = new YhKhjbxxView();
        YhKhjbxx yhKhjbxx = this.getById(yhbh);
        if (yhKhjbxx == null) throw new FeesException("用户不存在", FeesErrorCode.BUSINESS_ERROR);
        BeanUtils.copyProperties(yhKhjbxx, view);
        return view;
    }

    @Override
    public void batchSave(List<YhKhjbxxInsert> userList) {
        userList.forEach(insert -> this.save(insert));
    }

    @Override
    public List<YhCbkxxSelect> getCbkxxById(String yhbh) {
        return yhCbkxxService.getList(yhbh);
    }

    @Transactional
    @Override
    public boolean disable(String yhbh) {
        YhKhjbxx oldYhKhjbxx = getById(yhbh);
        if (oldYhKhjbxx == null) throw new FeesException("用户不存在", FeesErrorCode.BUSINESS_ERROR);
        YhKhjbxx yhKhjbxx = new YhKhjbxx();
        BeanUtils.copyProperties(oldYhKhjbxx, yhKhjbxx);
        yhKhjbxx.setYhzt(YhztEnum.作废用户);
        this.updateById(yhKhjbxx);

        boolean isupdate = false;
        List<YhCbkxx> cbkxx = yhCbkxxService.query().eq("yhbh", yhbh).list();
        if (!CollectionUtil.isEmpty(cbkxx)) {
            isupdate = yhCbkxxService.update().set("jfzt", "2").apply("yhbh='" + yhbh + "'").update();
        }
        if (isupdate) {
            List<YyCblr> cblrs = yyCblrService.query().eq("yhbh", yhbh).list();
            if (!CollectionUtil.isEmpty(cblrs)) {
                isupdate = yyCblrService.update().set("jfzt", "2").apply("yhbh='" + yhbh + "'").update();
            }
        }

        saveLog(yhKhjbxx, oldYhKhjbxx, "作废");
        return true;
    }

    //启用用户
    @Transactional
    @Override
    public boolean enable(String yhbh) {
        YhKhjbxx yhKhjbxx = getById(yhbh);
        if (yhKhjbxx == null) throw new FeesException("用户不存在", FeesErrorCode.BUSINESS_ERROR);
        yhKhjbxx.setYhzt(YhztEnum.正式用户);

        boolean isupdate = false;
        List<YhCbkxx> cbkxx = yhCbkxxService.query().eq("yhbh", yhbh).list();
        if (!CollectionUtil.isEmpty(cbkxx)) {
            isupdate = yhCbkxxService.update().set("jfzt", "1").apply("yhbh='" + yhbh + "'").update();
        }
        if (isupdate) {
            List<YyCblr> cblrs = yyCblrService.query().eq("yhbh", yhbh).list();
            if (!CollectionUtil.isEmpty(cblrs)) {
                isupdate = yyCblrService.update().set("jfzt", "1").apply("yhbh='" + yhbh + "'").update();
            }
        }

        this.updateById(yhKhjbxx);
        return true;
    }

    @Override
    public boolean delById(String yhbh) {
        YhKhjbxx yhKhjbxx = this.getById(yhbh);
        if (yhKhjbxx == null) throw new FeesException("用户不存在", FeesErrorCode.BUSINESS_ERROR);
        saveLog(null, yhKhjbxx, "删除");
        this.removeById(yhbh);
        return true;
    }

    @Override
    public boolean delByIds(List<String> yhbhs) {
        yhbhs.forEach(yhbh -> delById(yhbh));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSscbb(YhKhjbxxSscbbUpdate update) {
        List<String> yhbhs = update.getYhbhs();
        if (update.getType().equals("1") || update.getType().equals("3")) {
            if (yhbhs == null || yhbhs.size() == 0) {
                throw new FeesException("请选择用户", FeesErrorCode.BUSINESS_ERROR);
            } else {
                yhbhs.forEach(yhbh -> {
                    YhKhjbxx yhKhjbxx = this.getById(yhbh);
                    if (yhKhjbxx == null)
                        throw new FeesException("[" + yhbh + "]用户不存在", FeesErrorCode.BUSINESS_ERROR);
                });
            }
        }

        String sscbpq = update.getSscbpq();
        String cbpqstr = null;
        String sscbb = update.getSscbb();
        if (update.getType().equals("2") || update.getType().equals("3") || update.getType().equals("4")) {
            if (sscbpq != null) {
                BmCbpq cbpq = cbpqService.getById(sscbpq);
                if (cbpq == null)
                    throw new FeesException("抄表片区不存在", FeesErrorCode.BUSINESS_ERROR);
            }
        }
        if (update.getType().equals("1") || update.getType().equals("2") || update.getType().equals("4")) {
            if (StringUtils.isEmpty(sscbb)) {
                throw new FeesException("请选择抄表本(册)", FeesErrorCode.BUSINESS_ERROR);
            }
            if (sscbb != null) {
                BmCbb cbb = cbbService.getById(sscbb);
                if (cbb == null)
                    throw new FeesException("抄表本(册)不存在", FeesErrorCode.BUSINESS_ERROR);
                if (update.getType().equals("1") || update.getType().equals("2"))
                    cbpqstr = cbb.getSscbpq();
            }
        }
        switch (update.getType()) {
            case "1"://选择分配
                this.lambdaUpdate()
                        .in(YhKhjbxx::getYhbh, yhbhs)
                        .set(YhKhjbxx::getSscbpq, cbpqstr)
                        .set(YhKhjbxx::getSscbb, sscbb)
                        .update();
                yyCblrService.lambdaUpdate()
                        .in(YyCblr::getYhbh, yhbhs)
                        .set(YyCblr::getSscbpq, cbpqstr)
                        .set(YyCblr::getSscbb, sscbb)
                        .update();
                break;
            case "2"://全部分配
                this.lambdaUpdate()
                        .isNull(YhKhjbxx::getSscbb)
                        .eq(StringUtils.isNotEmpty(sscbpq), YhKhjbxx::getSscbpq, sscbpq)
                        .set(YhKhjbxx::getSscbpq, cbpqstr)
                        .set(YhKhjbxx::getSscbb, sscbb)
                        .update();
                yyCblrService.lambdaUpdate()
                        .isNull(YyCblr::getSscbb)
                        .eq(StringUtils.isNotEmpty(sscbpq), YyCblr::getSscbpq, sscbpq)
                        .set(YyCblr::getSscbpq, cbpqstr)
                        .set(YyCblr::getSscbb, sscbb)
                        .update();
                break;
            case "3"://选择撤销
                this.lambdaUpdate()
                        .in(YhKhjbxx::getYhbh, yhbhs)
                        .set(YhKhjbxx::getSscbpq, sscbpq)
                        .set(YhKhjbxx::getSscbb, "")
                        .update();
                yyCblrService.lambdaUpdate()
                        .in(YyCblr::getYhbh, yhbhs)
                        .set(YyCblr::getSscbpq, sscbpq)
                        .set(YyCblr::getSscbb, "")
                        .update();
                break;
            case "4"://全部撤销
                this.lambdaUpdate()
                        .in(YhKhjbxx::getSscbb, sscbb)
                        .set(YhKhjbxx::getSscbpq, sscbpq)
                        .set(YhKhjbxx::getSscbb, "")
                        .update();
                yyCblrService.lambdaUpdate()
                        .in(YyCblr::getSscbb, sscbb)
                        .set(YyCblr::getSscbpq, sscbpq)
                        .set(YyCblr::getSscbb, "")
                        .update();
                break;
        }
        return true;
    }

    @Override
    public PageResult<YhkhjbxxSelect2> getPage2(PageDto pageDto, YhkhjbxxFilter filter) {
        List<Order> orders = pageDto.getOrders();
        orders = orders.stream().map(item -> {
            if (item.getColumn().equals("yhbh")) {
                Order order = new Order();
                order.setColumn("a.yhbh");
                order.setAsc(true);
                return order;
            }
            return item;
        }).collect(Collectors.toList());
        pageDto.setOrders(orders);

//        if (CollUtil.isNotEmpty(orders)) {
//            orders.forEach(o -> {
//                String column = o.getColumn();
//                if ("jdrq".equalsIgnoreCase(column) && !o.isAsc()) {
//                    filter.setJdrqdxpx(1);
//                }
//            });
//        }

        PageDto<YhkhjbxxSelect2> page = baseMapper.getPage2(pageDto, filter);
        return page.toVo();
    }

    @Override
    public PageResult<YhkhjbxxSelect2> getUserInfoPage(PageDto pageDto, YhkhjbxxFilter filter) {
        PageDto<YhkhjbxxSelect2> page = baseMapper.getUserInfoPage(pageDto, filter);
        return page.toVo();
    }

    @Override
    public List<YhKhjbxx> getListByYhbh(List<String> yhbhs) {
        if (yhbhs == null || yhbhs.isEmpty()) return new ArrayList<>();
        return this.list(Wrappers.lambdaQuery(YhKhjbxx.class).in(YhKhjbxx::getYhbh, yhbhs));
    }

    @Override
    public PageDto<YhkhjbxxSelect> getPage_cbbwfp(PageDto page, YhkhjbxxCbbWfpFilter filter) {
        return this.baseMapper.getPage_cbbwfp(page, filter);
    }

    @Override
    public PageDto<YhkhjbxxSelect> getPage_cbbyfp(PageDto page, YhkhjbxxCbbWfpFilter filter) {
        return this.baseMapper.getPage_cbbyfp(page, filter);
    }

    @Override
    public PageResult<YhNdjtslSelect> getNdjtslxx(PageDto pageDto, String yhbh) {
        List<Order> orders = pageDto.getOrders();
        if (orders.size() == 0) {
            orders.add(new Order("yhbh", true));
            orders.add(new Order("jtxh", true));
            pageDto.setOrders(orders);
        }
        PageDto<YhNdjtslSelect> page = this.baseMapper.getNdjtslxx(pageDto, yhbh);
        PageResult<YhNdjtslSelect> pageResult = page.toVo();
        pageResult.getRecords().forEach(item -> {
            BigDecimal rksl = item.getRksl();
            BigDecimal jtzs = item.getJtzs();
            BigDecimal jtrksl = item.getJtrksl();
            BigDecimal jtrkjs = item.getJtrkjs();
            BigDecimal nzlsl = item.getNdljl();
            //计算接各个阶梯
            if (rksl.compareTo(jtrkjs) > 0) {
                if (item.getJtxh().equals("1")) {
                    item.setJtsl(jtzs.add(jtrksl.multiply(rksl.subtract(jtrkjs))));
                } else if (item.getJtxh().equals("2")) {
                    item.setJtsl(jtzs.add(jtrksl.multiply(rksl.subtract(jtrkjs)).multiply(BigDecimal.valueOf(2))));
                } else {
                    item.setJtsl(jtzs);
                }
            } else {
                item.setJtsl(jtzs);
            }
            //计算各个阶梯剩余量
            if (item.getJtxh().equals("1")) {
                if (item.getJtsl().compareTo(nzlsl) > 0) {
                    item.setSysl(item.getJtsl().subtract(nzlsl));
                    item.setYysl(nzlsl);
                } else {
                    item.setSysl(BigDecimal.ZERO);
                    item.setYysl(item.getJtsl());
                }
            }
            if (item.getJtxh().equals("2")) {
                //获取上一阶梯水量
                BigDecimal syjtsl = pageResult.getRecords().stream().filter(
                                item1 -> item1.getJtxh().equals("1") &&
                                        item1.getYhbh().equals(item.getYhbh()))
                        .map(YhNdjtslSelect::getJtsl).reduce(BigDecimal.ZERO, BigDecimal::add);
                if (syjtsl.compareTo(nzlsl) > 0) {
                    item.setSysl(item.getJtsl());
                    item.setYysl(BigDecimal.ZERO);
                } else {
                    if (item.getJtsl().compareTo(nzlsl) > 0) {
                        item.setSysl(item.getJtsl().subtract(nzlsl));
                        item.setYysl(nzlsl.subtract(syjtsl));
                    } else {
                        item.setSysl(BigDecimal.ZERO);
                        item.setYysl(item.getJtsl().subtract(syjtsl));
                    }
                }
            }
            if (item.getJtxh().equals("3")) {
                //获取上一阶梯水量
                BigDecimal syjtsl = pageResult.getRecords().stream().filter(
                                item1 -> (item1.getJtxh().equals("2")) &&
                                        item1.getYhbh().equals(item.getYhbh()))
                        .map(YhNdjtslSelect::getJtsl).reduce(BigDecimal.ZERO, BigDecimal::add);
                if (syjtsl.compareTo(nzlsl) > 0) {
                    item.setYysl(BigDecimal.ZERO);
                } else {
                    item.setYysl(nzlsl.subtract(syjtsl));
                }
                item.setSysl(item.getJtsl());
            }
        });
        return pageResult;
    }

    @Override
    public List<YhNdjtslSelect> getNdjtslxxlist(String yhbh) {
        List<YhNdjtslSelect> list = this.baseMapper.getNdjtslxx(yhbh);
        if (!list.isEmpty()) {
            list.stream().sorted(Comparator.comparing(YhNdjtslSelect::getYhbh)
                    .thenComparing(YhNdjtslSelect::getJtxh)
                    .reversed()).collect(Collectors.toList());
            list.forEach(item -> {
                BigDecimal rksl = item.getRksl();
                BigDecimal jtzs = item.getJtzs();
                BigDecimal jtrksl = item.getJtrksl();
                BigDecimal jtrkjs = item.getJtrkjs();
                BigDecimal nzlsl = item.getNdljl();
                //计算接各个阶梯
                if (rksl.compareTo(jtrkjs) > 0) {
                    if (item.getJtxh().equals("1")) {
                        item.setJtsl(jtzs.add(jtrksl.multiply(rksl.subtract(jtrkjs))));
                    } else if (item.getJtxh().equals("2")) {
                        item.setJtsl(jtzs.add(jtrksl.multiply(rksl.subtract(jtrkjs)).multiply(BigDecimal.valueOf(2))));
                    } else {
                        item.setJtsl(jtzs);
                    }
                } else {
                    item.setJtsl(jtzs);
                }
                //计算各个阶梯剩余量
                if (item.getJtxh().equals("1")) {
                    if (item.getJtsl().compareTo(nzlsl) > 0) {
                        item.setSysl(item.getJtsl().subtract(nzlsl));
                        item.setYysl(nzlsl);
                    } else {
                        item.setSysl(BigDecimal.ZERO);
                        item.setYysl(item.getJtsl());
                    }
                }
                if (item.getJtxh().equals("2")) {
                    //获取上一阶梯水量
                    BigDecimal syjtsl = list.stream().filter(
                                    item1 -> item1.getJtxh().equals("1") &&
                                            item1.getYhbh().equals(item.getYhbh()))
                            .map(YhNdjtslSelect::getJtsl).reduce(BigDecimal.ZERO, BigDecimal::add);
                    if (syjtsl.compareTo(nzlsl) > 0) {
                        item.setSysl(item.getJtsl());
                        item.setYysl(BigDecimal.ZERO);
                    } else {
                        if (item.getJtsl().compareTo(nzlsl) > 0) {
                            item.setSysl(item.getJtsl().subtract(nzlsl));
                            item.setYysl(nzlsl.subtract(syjtsl));
                        } else {
                            item.setSysl(BigDecimal.ZERO);
                            item.setYysl(item.getJtsl().subtract(syjtsl));
                        }
                    }
                }
                if (item.getJtxh().equals("3")) {
                    //获取上一阶梯水量
                    BigDecimal syjtsl = list.stream().filter(
                                    item1 -> (item1.getJtxh().equals("2")) &&
                                            item1.getYhbh().equals(item.getYhbh()))
                            .map(YhNdjtslSelect::getJtsl).reduce(BigDecimal.ZERO, BigDecimal::add);
                    if (syjtsl.compareTo(nzlsl) > 0) {
                        item.setYysl(BigDecimal.ZERO);
                    } else {
                        item.setYysl(nzlsl.subtract(syjtsl));
                    }
                    item.setSysl(item.getJtsl());
                }
                if (item.getJtxh().equals("3")) {
                    //获取上一阶梯水量
                    BigDecimal syjtsl = list.stream().filter(
                                    item1 -> (item1.getJtxh().equals("2")) &&
                                            item1.getYhbh().equals(item.getYhbh()))
                            .map(YhNdjtslSelect::getJtsl).reduce(BigDecimal.ZERO, BigDecimal::add);
                    if (syjtsl.compareTo(nzlsl) > 0) {
                        item.setYysl(BigDecimal.ZERO);
                    } else {
                        item.setYysl(nzlsl.subtract(syjtsl));
                    }
                    item.setSysl(item.getJtsl());
                }
            });


        }
        return list;
    }

    private void check(YhKhjbxx yhKhjbxx, boolean insert) {
        String yhbh = yhKhjbxx.getYhbh();
        if (insert) {
            int count = this.baseMapper.selectExistYhbh(yhbh);
            if (count > 0) throw new FeesException("用户编号已存在", FeesErrorCode.BUSINESS_ERROR);
        }
        if (StringUtils.isEmpty(yhKhjbxx.getSsgs())) {
            throw new FeesException("公司不能为空", FeesErrorCode.BUSINESS_ERROR);
        }
        if (StringUtils.isEmpty(yhKhjbxx.getSscbpq())) {
            throw new FeesException("片区不能为空", FeesErrorCode.BUSINESS_ERROR);
        }
        if (StringUtils.isEmpty(yhKhjbxx.getSscbb())) {
            throw new FeesException("抄表本(册)不能为空", FeesErrorCode.BUSINESS_ERROR);
        }
        if (StringUtils.isEmpty(yhKhjbxx.getYhxm())) {
            throw new FeesException("用户姓名不能为空", FeesErrorCode.BUSINESS_ERROR);
        }
        if (StringUtils.isEmpty(yhKhjbxx.getYhdz())) {
            throw new FeesException("用户地址不能为空", FeesErrorCode.BUSINESS_ERROR);
        }
    }


    /**
     * 用户档案操作日志记录
     *
     * @param yhKhjbxx
     * @param oldyhKhjbxx
     * @param logCzlx
     */
    @Override
    public void saveLog(YhKhjbxx yhKhjbxx, YhKhjbxx oldyhKhjbxx, String logCzlx) {
        if (yhKhjbxx.equals(oldyhKhjbxx)) {
            return;
        }
        String logCzrybm = String.valueOf(SecurityUtils.getUserId());
        String logCzryxm = SecurityUtils.getUser().getRealName();
        //档案操作日志记录
        Date logCzsj = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String logLsh = dateFormat.format(logCzsj);
        String log_bz = "";
        if (oldyhKhjbxx != null) {
//            YhKhjbxxLog before = new YhKhjbxxLog();
//            BeanUtils.copyProperties(oldYhKhjbxx, before);
//            before.setCzsj(logCzsj);//操作时间
//            before.setLogLsh(logLsh);//流水号
//            before.setLogCzryxm(logCzryxm);//操作人员
//            before.setLogCzrybm(logCzrybm);//操作人员编码
//            before.setLogCzsj(logCzsj);
//            before.setLogId(GuidGenerator.getGuid());//id
//            before.setLogCzlx(logCzlx);
//            before.setLogCllx(String.format("%s前", logCzlx));
//            yhKhjbxxLogService.save(before);
            log_bz = toolsUtils.compareFieldDiffToJson(oldyhKhjbxx, yhKhjbxx);
        }
        if (yhKhjbxx != null) {
            YhKhjbxxLog after = new YhKhjbxxLog();
            BeanUtils.copyProperties(yhKhjbxx, after);
            after.setCzsj(logCzsj);//操作时间
            after.setLogLsh(logLsh);//流水号
            after.setLogCzryxm(logCzryxm);//操作人员
            after.setLogCzrybm(logCzrybm);//操作人员编码
            after.setLogCzsj(logCzsj);
            after.setLogId(GuidGenerator.getGuid());
            after.setLogCzlx(logCzlx);
            after.setLogCllx(String.format("%s后", logCzlx));
            after.setLogBz(log_bz);
            yhKhjbxxLogService.save(after);
        }
    }

    @Override
    public PageResult<YhwjSelect> getwjPage(PageDto pageDto, YhwjFilter filter) {
        PageDto<YhwjSelect> result = this.baseMapper.getwjPage(pageDto, filter);
        return result.toVo();
    }

    @Override
    public List<YhkhjbxxXzyhSelect> xzyhtj(YhkhjbxxXzyhFilter filter) {
        //查询出原始list数据
        List<YhkhjbxxXzyhSelect> list = this.baseMapper.xzyhtj(filter);
        List<YhkhjbxxXzyhSelect> resultlist = new ArrayList<>();
        if (list != null && list.size() > 0) {
            List<Company> companyList = companyService.list();
            list.forEach(item -> {
                item.setSsgsmc(companyList.stream().filter(e -> e.getId().equals(Long.valueOf(item.getSsgsmc()))).findFirst().get().getCompanyName());
                Integer hj = item.getSbkj15() + item.getSbkj20() + item.getSbkj25() + item.getSbkj30() + item.getSbkj32() + item.getSbkj40()
                        + item.getSbkj50() + item.getSbkj65() + item.getSbkj75() + item.getSbkj80() + item.getSbkj90() + item.getSbkj100()
                        + item.getSbkj110() + item.getSbkj150() + item.getSbkj160() + item.getSbkj200() + item.getSbkj300();
                item.setHj(hj);
            });
            List<String> ssgsmcList = list.stream().map(YhkhjbxxXzyhSelect::getSsgsmc).distinct().collect(Collectors.toList());
            for (String ssgsmc : ssgsmcList) {
                List<YhkhjbxxXzyhSelect> ssgsmclist = list.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).collect(Collectors.toList());
                YhkhjbxxXzyhSelect yhkhjbxxXzyhSelect = new YhkhjbxxXzyhSelect();
                yhkhjbxxXzyhSelect.setSsgsmc(ssgsmc);
                yhkhjbxxXzyhSelect.setYsxzmc("小计");
                yhkhjbxxXzyhSelect.setSbkj15(ssgsmclist.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).map(YhkhjbxxXzyhSelect::getSbkj15).reduce(0, Integer::sum));
                yhkhjbxxXzyhSelect.setSbkj20(ssgsmclist.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).map(YhkhjbxxXzyhSelect::getSbkj20).reduce(0, Integer::sum));
                yhkhjbxxXzyhSelect.setSbkj25(ssgsmclist.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).map(YhkhjbxxXzyhSelect::getSbkj25).reduce(0, Integer::sum));
                yhkhjbxxXzyhSelect.setSbkj30(ssgsmclist.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).map(YhkhjbxxXzyhSelect::getSbkj30).reduce(0, Integer::sum));
                yhkhjbxxXzyhSelect.setSbkj32(ssgsmclist.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).map(YhkhjbxxXzyhSelect::getSbkj32).reduce(0, Integer::sum));
                yhkhjbxxXzyhSelect.setSbkj40(ssgsmclist.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).map(YhkhjbxxXzyhSelect::getSbkj40).reduce(0, Integer::sum));
                yhkhjbxxXzyhSelect.setSbkj50(ssgsmclist.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).map(YhkhjbxxXzyhSelect::getSbkj50).reduce(0, Integer::sum));
                yhkhjbxxXzyhSelect.setSbkj65(ssgsmclist.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).map(YhkhjbxxXzyhSelect::getSbkj65).reduce(0, Integer::sum));
                yhkhjbxxXzyhSelect.setSbkj75(ssgsmclist.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).map(YhkhjbxxXzyhSelect::getSbkj75).reduce(0, Integer::sum));
                yhkhjbxxXzyhSelect.setSbkj80(ssgsmclist.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).map(YhkhjbxxXzyhSelect::getSbkj80).reduce(0, Integer::sum));
                yhkhjbxxXzyhSelect.setSbkj90(ssgsmclist.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).map(YhkhjbxxXzyhSelect::getSbkj90).reduce(0, Integer::sum));
                yhkhjbxxXzyhSelect.setSbkj100(ssgsmclist.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).map(YhkhjbxxXzyhSelect::getSbkj100).reduce(0, Integer::sum));
                yhkhjbxxXzyhSelect.setSbkj110(ssgsmclist.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).map(YhkhjbxxXzyhSelect::getSbkj110).reduce(0, Integer::sum));
                yhkhjbxxXzyhSelect.setSbkj150(ssgsmclist.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).map(YhkhjbxxXzyhSelect::getSbkj150).reduce(0, Integer::sum));
                yhkhjbxxXzyhSelect.setSbkj160(ssgsmclist.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).map(YhkhjbxxXzyhSelect::getSbkj160).reduce(0, Integer::sum));
                yhkhjbxxXzyhSelect.setSbkj200(ssgsmclist.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).map(YhkhjbxxXzyhSelect::getSbkj200).reduce(0, Integer::sum));
                yhkhjbxxXzyhSelect.setSbkj300(ssgsmclist.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).map(YhkhjbxxXzyhSelect::getSbkj300).reduce(0, Integer::sum));
                yhkhjbxxXzyhSelect.setHj(ssgsmclist.stream().filter(e -> e.getSsgsmc().equals(ssgsmc)).map(YhkhjbxxXzyhSelect::getHj).reduce(0, Integer::sum));
                resultlist.addAll(ssgsmclist);
                resultlist.add(yhkhjbxxXzyhSelect);
            }
            YhkhjbxxXzyhSelect yhkhjbxxXzyhSelect = new YhkhjbxxXzyhSelect();
            yhkhjbxxXzyhSelect.setSsgsmc("总计");
            yhkhjbxxXzyhSelect.setYsxzmc("总计");
            yhkhjbxxXzyhSelect.setSbkj15(list.stream().map(YhkhjbxxXzyhSelect::getSbkj15).reduce(0, Integer::sum));
            yhkhjbxxXzyhSelect.setSbkj20(list.stream().map(YhkhjbxxXzyhSelect::getSbkj20).reduce(0, Integer::sum));
            yhkhjbxxXzyhSelect.setSbkj25(list.stream().map(YhkhjbxxXzyhSelect::getSbkj25).reduce(0, Integer::sum));
            yhkhjbxxXzyhSelect.setSbkj30(list.stream().map(YhkhjbxxXzyhSelect::getSbkj30).reduce(0, Integer::sum));
            yhkhjbxxXzyhSelect.setSbkj32(list.stream().map(YhkhjbxxXzyhSelect::getSbkj32).reduce(0, Integer::sum));
            yhkhjbxxXzyhSelect.setSbkj40(list.stream().map(YhkhjbxxXzyhSelect::getSbkj40).reduce(0, Integer::sum));
            yhkhjbxxXzyhSelect.setSbkj50(list.stream().map(YhkhjbxxXzyhSelect::getSbkj50).reduce(0, Integer::sum));
            yhkhjbxxXzyhSelect.setSbkj65(list.stream().map(YhkhjbxxXzyhSelect::getSbkj65).reduce(0, Integer::sum));
            yhkhjbxxXzyhSelect.setSbkj75(list.stream().map(YhkhjbxxXzyhSelect::getSbkj75).reduce(0, Integer::sum));
            yhkhjbxxXzyhSelect.setSbkj80(list.stream().map(YhkhjbxxXzyhSelect::getSbkj80).reduce(0, Integer::sum));
            yhkhjbxxXzyhSelect.setSbkj90(list.stream().map(YhkhjbxxXzyhSelect::getSbkj90).reduce(0, Integer::sum));
            yhkhjbxxXzyhSelect.setSbkj100(list.stream().map(YhkhjbxxXzyhSelect::getSbkj100).reduce(0, Integer::sum));
            yhkhjbxxXzyhSelect.setSbkj110(list.stream().map(YhkhjbxxXzyhSelect::getSbkj110).reduce(0, Integer::sum));
            yhkhjbxxXzyhSelect.setSbkj150(list.stream().map(YhkhjbxxXzyhSelect::getSbkj150).reduce(0, Integer::sum));
            yhkhjbxxXzyhSelect.setSbkj160(list.stream().map(YhkhjbxxXzyhSelect::getSbkj160).reduce(0, Integer::sum));
            yhkhjbxxXzyhSelect.setSbkj200(list.stream().map(YhkhjbxxXzyhSelect::getSbkj200).reduce(0, Integer::sum));
            yhkhjbxxXzyhSelect.setSbkj300(list.stream().map(YhkhjbxxXzyhSelect::getSbkj300).reduce(0, Integer::sum));
            yhkhjbxxXzyhSelect.setHj(list.stream().map(YhkhjbxxXzyhSelect::getHj).reduce(0, Integer::sum));
            resultlist.add(yhkhjbxxXzyhSelect);
        }
        return resultlist;
    }

    @Override
    public YhKhjbxx selectByKhbm(String khbm) {
        if (StrUtil.isEmpty(khbm)) {
            throw new FeesException("大户客户编码不能为空", FeesErrorCode.BUSINESS_ERROR);
        }

        LambdaQueryWrapper<YhKhjbxx> queryWrapper = Wrappers.<YhKhjbxx>lambdaQuery().eq(YhKhjbxx::getKhbm, khbm)
                .eq(YhKhjbxx::getZhbz, 1);

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public ResultRespone pldr(MultipartFile file, ImportDto importDto) throws Exception {
        List<YhkhjbxxExcel> userList = EasyExcel.read(file.getInputStream())
                .head(YhkhjbxxExcel.class).sheet().doReadSync();

        List<BmSj> bmSjs = bmSjService.selectListMap();
        if (CollectionUtil.isEmpty(bmSjs)) {
            throw new FeesException("请先添加水价信息", FeesErrorCode.BUSINESS_ERROR);
        }
        Map<String, BmSj> sjNameMap = bmSjs.stream().collect(Collectors.toMap(BmSj::getName, o -> o));

        Integer sucess = 0;
        Integer fail = 0;
        StringBuilder builder = new StringBuilder();
        int index = 1;

        for (YhkhjbxxExcel user : userList) {
            boolean issuccess = checkParameter(user, builder, index, sjNameMap);
            if (!issuccess) {
                index++;
                fail++;
                continue;
            }

            SimpleDateFormat dFormat = new SimpleDateFormat(DatePattern.NORM_DATE_PATTERN);
            String yhbh = user.getYhbh();
            String putmaxyhbh = yhbh;
            user.setYhbh(String.valueOf(putmaxyhbh));
            String strplbh = GuidGenerator.getGuid();

            // 1、创建用户档案
            YhKhjbxx yhKhjbxx = pljdcjkhjbxx(strplbh, importDto, user, dFormat);

            // 2、创建水表档案(抄表卡信息)
            String sjmc = user.getSjmc();
            BmSj bmSj = sjNameMap.get(sjmc);
            YhCbkxx cbkxx = pljdcjcbkxx(strplbh, user, importDto, dFormat, bmSj);
            String sblb = cbkxx.getSblb();

            // 3、用户和水表添加日志
            this.saveLog(yhKhjbxx, null, "添加");//添加用户档案日志
            yhCbkxxService.saveLog(cbkxx, null, "添加");//添加水表档案日志
//            DaCustomerAndMeter daCustomerAndMeter = new DaCustomerAndMeter();
//            daCustomerAndMeter.setYhKhjbxx(yhKhjbxx);
//            daCustomerAndMeter.setYhCbkxx(cbkxx);
//            yhKhjbxxLogService.addDaCustomerAndMeterLog(daCustomerAndMeter, "save");

            // 4、抄表计划添加
            if (sblb.equals("0") || sblb.equals("2") || sblb.equals("3") || sblb.equals("4")) {
                pljdcjcblr(importDto, user, yhKhjbxx, cbkxx);
            }
            index++;
            sucess++;
        }

        String msg = "[导入成功:" + sucess + "条,导入失败:" + fail + "条]";
        if (builder.length() > 0) {
            msg = msg + SysConstant.JDCWTSC3 + builder.toString() + SysConstant.JDCWTSC4;
            ResultRespone<T> apiResult = new ResultRespone();
            apiResult.setCode(Integer.valueOf(4));
            apiResult.setResult(null);
            apiResult.setMessage(msg);
            return apiResult;
        }
        return ResultRespone.success(null, msg);
    }

    private void pljdcjcblr(ImportDto importDto, YhkhjbxxExcel user, YhKhjbxx yhKhjbxx, YhCbkxx cbkxx) {
        YyCblr bwMeterReadPlan = new YyCblr();
        String yhbh = user.getYhbh();
        bwMeterReadPlan.setYhbh(String.valueOf(yhbh));//用户编号
        bwMeterReadPlan.setCbkh("1");//抄表卡号
        bwMeterReadPlan.setCbkid(yhbh + "1");//抄表卡ID
        Map<String, Object> mapcwrq = baseMapper.selectCwrq();//获取财务日期
        String strcwrq = mapcwrq.get("cwrq").toString();
        bwMeterReadPlan.setCwrq(strcwrq);//财务日期
        bwMeterReadPlan.setJsbz(JsbzEnum.未算费);//计算标志
        bwMeterReadPlan.setJfzt("1");//正常状态
        bwMeterReadPlan.setCbbz(CbbzEnum.未抄表);//未抄表状态
        bwMeterReadPlan.setShbz(ShbzEnum.未审核);//未审核标志
        bwMeterReadPlan.setSsgs(importDto.getSsgs());//公司
        bwMeterReadPlan.setSscbpq(importDto.getSscbpq());
        bwMeterReadPlan.setSscbb(user.getSscbb());
        if (!StringUtils.isEmpty(user.getSbqs())) {
            bwMeterReadPlan.setScds(new BigDecimal(user.getSbqs()));
            bwMeterReadPlan.setBcds(new BigDecimal(user.getSbqs()));
        }
        yyCblrService.save(bwMeterReadPlan);
        //添加智能表档案日志
        String sblb = cbkxx.getSblb();
        if (sblb.equals("2") || sblb.equals("3")) {
            znbDaLogService.addznbDaLog(yhKhjbxx, cbkxx, "0");
        }
    }

    private YhCbkxx pljdcjcbkxx(String strplbh, YhkhjbxxExcel user, ImportDto importDto, SimpleDateFormat dFormat, BmSj bmSj) throws ParseException {
        YhCbkxx cbkxx = new YhCbkxx();
        cbkxx.setGxh(strplbh);//批次号
        cbkxx.setCbkh("1");

        String yhbh = user.getYhbh();
        cbkxx.setCbkid(yhbh + "1");
        cbkxx.setYhbh(yhbh);
        cbkxx.setCzrybm(importDto.getCzrybm());
        cbkxx.setCzryxm(importDto.getCzrymc());
        cbkxx.setCzsj(new Date());

        String jfzt = importDto.getJfzt();
        cbkxx.setLjffs(importDto.getLjffs());
        cbkxx.setLjfje(new BigDecimal(importDto.getLjfje()));
        if (jfzt.equals("1") || jfzt.equals("正常")) {
            cbkxx.setJfzt("1");
        } else {
            cbkxx.setJfzt("2");
        }

        cbkxx.setSbsccj(importDto.getSbsccj());
        if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(user.getSbccrq())) {
            cbkxx.setSbccrq(dFormat.parse(user.getSbccrq()));
        }

        String sblb = importDto.getSblb();
        cbkxx.setSblb(sblb);
        cbkxx.setSbbh(user.getSbbh());
        cbkxx.setSbkj(user.getSbkj());

        // 水价编码
        cbkxx.setSjbm(bmSj.getCode());

        cbkxx.setSbxh(importDto.getSbxh());
        cbkxx.setCbpl(importDto.getCbpl());
        cbkxx.setSblc(importDto.getSblc());
        cbkxx.setBjwz(importDto.getBjwz());

        cbkxx.setSbqs(Integer.parseInt(user.getSbqs()));
        cbkxx.setWsfbz(importDto.getWsfbz());
        cbkxx.setLjfbz(importDto.getLjfbz());
        cbkxx.setSzyfbz("1");
        yhCbkxxService.save(cbkxx);
        return cbkxx;
    }

    private YhKhjbxx pljdcjkhjbxx(String strplbh, ImportDto importDto, YhkhjbxxExcel user, SimpleDateFormat dFormat) throws ParseException {

        YhKhjbxx yhKhjbxx = new YhKhjbxx();
        yhKhjbxx.setJdpcxlh(strplbh);
        yhKhjbxx.setSsgs(importDto.getSsgs());
        yhKhjbxx.setSscbpq(importDto.getSscbpq());
        yhKhjbxx.setSscbb(user.getSscbb());

        String yhbh = user.getYhbh();
        yhKhjbxx.setYhbh(yhbh);
        yhKhjbxx.setYkhbh(yhbh);

        yhKhjbxx.setYhxm(user.getYhxm());
        yhKhjbxx.setYhdz(user.getYhdz());
        yhKhjbxx.setLxdh(user.getLxdh());

        yhKhjbxx.setFpName(user.getYhxm());
        yhKhjbxx.setFpDz(user.getYhdz());
        yhKhjbxx.setFpLxdh(user.getLxdh());

        String yhzt = importDto.getYhzt();
        yhKhjbxx.setZjhm(user.getZjhm());//证件号码
        if (yhzt.equals("1") || yhzt.equals("正式用户")) {
            yhKhjbxx.setYhzt(YhztEnum.正式用户);//用户状态
        }
        if (yhzt.equals("0") || yhzt.equals("申请用户")) {
            yhKhjbxx.setYhzt(YhztEnum.申请用户);//用户状态
        }
        if (yhzt.equals("2") || yhzt.equals("报停用户")) {
            yhKhjbxx.setYhzt(YhztEnum.报停用户);//用户状态
        }
        if (yhzt.equals("3") || yhzt.equals("销户用户")) {
            yhKhjbxx.setYhzt(YhztEnum.销户用户);//用户状态
        }
        if (yhzt.equals("4") || yhzt.equals("作废用户")) {
            yhKhjbxx.setYhzt(YhztEnum.作废用户);//用户状态
        }

        yhKhjbxx.setTsbz(importDto.getTsbz());
        yhKhjbxx.setFffs("1");
        // 通水时间
        String tsrq = importDto.getTsrq();
        if (StrUtil.isNotEmpty(tsrq) && !tsrq.equals("null")) {
            yhKhjbxx.setTsrq(dFormat.parse(tsrq));
        }
        //申请日期
        if (StrUtil.isNotEmpty(user.getSqrq())) {
            yhKhjbxx.setSqrq(dFormat.parse(user.getSqrq()));
        }
        String czrymc = importDto.getCzrymc();
        String czrybm = importDto.getCzrybm();
        yhKhjbxx.setCzsj(new DateTime());//创建时间
        yhKhjbxx.setJdrq(new DateTime());
        yhKhjbxx.setCzrybm(czrybm); //创建人员编码
        yhKhjbxx.setCzryxm(czrymc); //创建人员名称
        yhKhjbxx.setJdrybm(czrybm); //操作人员
        yhKhjbxx.setJdryxm(czrymc); //操作人员名称

        Date currentDate = new Date();
        yhKhjbxx.setCzsj(currentDate);//操作时间

        yhKhjbxx.setZjlb("1");//证件类型

        String khbm = user.getKhbm();
        if (StrUtil.isEmpty(khbm)) {
            yhKhjbxx.setDxh(YhDxhEnum.XH.getCode());
        } else {
            LambdaQueryWrapper<YhKhjbxx> wrapper = Wrappers.<YhKhjbxx>lambdaQuery().eq(YhKhjbxx::getKhbm, khbm);
            Long count = baseMapper.selectCount(wrapper);

            if (count > 0) {
                yhKhjbxx.setZhbz(0);
            } else {
                yhKhjbxx.setZhbz(1);
            }
            yhKhjbxx.setDxh(YhDxhEnum.DH.getCode());
        }

        this.save(yhKhjbxx);

        return yhKhjbxx;
    }

    private boolean checkParameter(YhkhjbxxExcel user, StringBuilder builder, int index, Map<String, BmSj> sjNameMap) {

        String yhbh = user.getYhbh();
        if (StrUtil.isEmpty(yhbh)) {
            builder.append(SysConstant.JDCWTSC1).append(index)
                    .append(SysConstant.JDCWTSC2).append(PljdcwxxEnum.YHBHWK.getValue());
            return false;
        }

        Boolean exisYh = baseMapper.exists(new QueryWrapper<YhKhjbxx>().eq("yhbh", yhbh));
        if (exisYh) {
            builder.append(SysConstant.JDCWTSC1).append(index)
                    .append(SysConstant.JDCWTSC2).append(PljdcwxxEnum.YHBHYCZ.getValue());
            return false;
        }

        if (StrUtil.isEmpty(user.getYhxm())) {
            builder.append(SysConstant.JDCWTSC1).append(index)
                    .append(SysConstant.JDCWTSC2).append(PljdcwxxEnum.YHXMWK.getValue());
            return false;
        }

        if (StrUtil.isEmpty(user.getYhdz())) {
            builder.append(SysConstant.JDCWTSC1).append(index)
                    .append(SysConstant.JDCWTSC2).append(PljdcwxxEnum.YHDZWK.getValue());
            return false;
        }

        if (StrUtil.isEmpty(user.getSbbh())) {
            builder.append(SysConstant.JDCWTSC1).append(index)
                    .append(SysConstant.JDCWTSC2).append(PljdcwxxEnum.YHSBHWK.getValue());
            return false;
        }

        if (StrUtil.isEmpty(user.getSbkj())) {
            builder.append(SysConstant.JDCWTSC1).append(index)
                    .append(SysConstant.JDCWTSC2).append(PljdcwxxEnum.YHSBKJWK.getValue());
            return false;
        }

        if (StrUtil.isEmpty(user.getSbqs())) {
            builder.append(SysConstant.JDCWTSC1).append(index)
                    .append(SysConstant.JDCWTSC2).append(PljdcwxxEnum.SBQDWK.getValue());
            return false;
        }

        if (StrUtil.isEmpty(user.getSqrq())) {
            builder.append(SysConstant.JDCWTSC1).append(index)
                    .append(SysConstant.JDCWTSC2).append(PljdcwxxEnum.SQRQWK.getValue());
            return false;
        }

        if (StrUtil.isEmpty(user.getSbccrq())) {
            builder.append(SysConstant.JDCWTSC1).append(index)
                    .append(SysConstant.JDCWTSC2).append(PljdcwxxEnum.SBCCRQWK.getValue());
            return false;
        }

        if (StrUtil.isEmpty(user.getSscbb())) {
            builder.append(SysConstant.JDCWTSC1).append(index)
                    .append(SysConstant.JDCWTSC2).append(PljdcwxxEnum.SBSSCBCWK.getValue());
            return false;
        }

        String sjmc = user.getSjmc();
        if (StrUtil.isEmpty(sjmc)) {
            builder.append(SysConstant.JDCWTSC1).append(index)
                    .append(SysConstant.JDCWTSC2).append(PljdcwxxEnum.YSXZMCWK.getValue());
            return false;
        }

        BmSj bmSj = sjNameMap.get(sjmc);
        if (ObjUtil.isNull(bmSj)) {
            builder.append(SysConstant.JDCWTSC1).append(index)
                    .append(SysConstant.JDCWTSC2).append(PljdcwxxEnum.SJWK.getValue());
            return false;
        }

        return true;
    }

    public boolean yhbhcc(String yhbh) {
        if (StrUtil.isEmpty(yhbh)) {
            return false;
        }

        LambdaQueryWrapper<YhKhjbxx> wrapper = Wrappers.<YhKhjbxx>lambdaQuery().eq(YhKhjbxx::getYhbh, yhbh);
        return baseMapper.selectCount(wrapper) > 0;
    }

}
