package com.unis.crk.common.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.crk.common.entity.Billnonew;
import com.unis.crk.common.mapper.BillnonewMapper;
import com.unis.crk.common.service.IBillnonewService;
import com.unis.crk.cplbusiness.entity.DataCrkywGysDefault;
import com.unis.crk.cplbusiness.service.IDataCrkywGysDefaultService;
import com.unis.crk.utils.FunUtils;
import lombok.Synchronized;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 单据序号生成表，中间表，专用于生成各种单据号 服务实现类
 * </p>
 *
 * @author TangSong
 * @since 2021-10-27
 */
@Service
public class BillnonewServiceImpl extends ServiceImpl<BillnonewMapper, Billnonew> implements IBillnonewService {


    @Autowired
    private IDataCrkywGysDefaultService dataCrkywGysDefaultService;
    @SuppressWarnings("unchecked")
    @Synchronized
    public String createNewNo(String prefix, String unitId, String tableName, String fieldName,
                              int NoLen) {
        String newvalue = "";
        try {
            String today = DateUtil.format(new Date(),"yyyyMMdd"); //得到当年日期
            QueryWrapper<Billnonew> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("max(BillNo) as BillNo");
            queryWrapper.eq("unitId", unitId);
            queryWrapper.eq("TABLE_NAME", tableName);
            queryWrapper.eq("FIELD_NAME", fieldName);
            queryWrapper.likeRight("BillNo", prefix+today); //查询当天
            Billnonew billnonew = super.getOne(queryWrapper);
            String bilNo = "";
            if (billnonew != null) {
                bilNo = billnonew.getBillNo();
            }
            newvalue = StringUtils.isBlank(bilNo) ?  prefix+ today + getNo("", NoLen) : getNo(bilNo, NoLen);
            //保存对象
            billnonew = new Billnonew();
            billnonew.setBillNo(newvalue);
            billnonew.setUnitId(unitId);
            billnonew.setTableName(tableName);
            billnonew.setFieldName(fieldName);
            if (!super.saveOrUpdate(billnonew)) {
                newvalue = "";
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return newvalue;
    }

    private String getNo(String expr, int len) {
        if (StringUtils.isBlank(expr)) {
            return String.format("%0" + len + "d", 1);
        }
        String temp = expr.substring(expr.length() - len);
        int no = 1;
        try {
            no = Integer.parseInt(temp);
        } catch (Exception localException) {
        }
        return expr.substring(0, expr.length() - len) + String.format("%0" + len + "d", no + 1);
    }

    @Override
    public String getKcbdDh(String unitId) {
        return createNewNo("kcbd"+unitId,unitId,"data_kcgl_kcbd_default","bddh",3);
    }
    @Override
    public String getCplQckcDh(String unitId) {
        return createNewNo("cplQckc"+unitId,unitId,"data_kcgl_cplqckc_default","qcbh",3);
    }

    @Override
    public String getCplTzdh(String unitId, Integer zylx) {
        String prefix = "CPLRK"; //默认成品粮入库
        if (zylx == 4) { //成品粮出库
            prefix = "CPLCK";
        }else if (zylx == 5) { //成品粮退换货入库
            prefix = "CPLTHRK";
        }else if (zylx == 6) { //成品粮退换货出库
            prefix = "CPLTHCK";
        }else if (zylx == 7) { //成品粮调拔入库
            prefix = "CPLDBRK";
        }else if (zylx == 8) { //成品粮调拔出库
            prefix = "CPLDBCK";
        }
        return createNewNo(prefix,unitId,"data_crkyw_cpltzdxx_default","tzdbm",4);
    }

    @Override
    public String getCplKcbdDh(String unitId) {
        return createNewNo("cplkcbd"+unitId,unitId,"data_kcgl_cplkcbd_default","bddh",3);
    }

    @Override
    public String getCplJydh(String unitId) {
        return  createNewNo("cpljy",unitId,"data_crkyw_cpljyxx_default","zljybhbh",3);
    }

    @Override
    public String getCplrkBizNo(String unitId) {
        return createNewNo("cplrk_"+unitId,unitId,"data_crkyw_cplrk_default","rkbh",3);
    }

    @Override
    public String getCplckBizNo(String unitId) {
        return createNewNo("cplck_"+unitId,unitId,"data_crkyw_cplck_default","ckbh",3);
    }

    @Override
    public String getScpc(String unitId, String spbh, String scrq) {
        String date =  DateUtil.format(DateUtil.parse(scrq,"yyyy-MM-dd"),"yyyyMMdd");
        return spbh+date;
    }
    @Override
    public String getScpc(String unitId, String spbh, String scrq,String gysId) {
        if (StringUtils.isNotBlank(gysId)){
            //使用供应商编码
            DataCrkywGysDefault dataCrkywGysDefault = dataCrkywGysDefaultService.getById(gysId);
            if (dataCrkywGysDefault != null){
                gysId = dataCrkywGysDefault.getSupplierNumber();
            }
        }
        String date =  DateUtil.format(DateUtil.parse(scrq,"yyyy-MM-dd"),"yyyyMMdd");
        return spbh+date+"_"+gysId;
    }

    @Override
    public String getQcrkBizNo(String unitId) {
        return createNewNo("qcrk_"+unitId,unitId,"data_crkyw_rmdj_default","BizNo",3);
    }

    @Override
    public String getQcckBizNo(String unitId) {
            return createNewNo("qcck_"+unitId,unitId,"data_crkyw_ckrmdj_default","BizNo",3);
    }

    @Override
    public String getQcrkQydh(String unitId) {
        return  createNewNo("",unitId,"data_crkyw_qyxx_default","qydh",3);
    }

    @Override
    public String getQcrkJjdh(String unitId) {
        return  createNewNo("",unitId,"data_crkyw_cmz_default","jjdh",3);
    }

    @Override
    public String getQcckJjdh(String unitId) {
        return  createNewNo("",unitId,"data_crkyw_ckcpz_default","jjdh",3);
    }

    @Override
    public String getQcrkJsdh(String unitId) {
        return  createNewNo("",unitId,"data_crkyw_jsxx_default","jsdh",3);
    }

    @Override
    public String getQcrkJydh(String unitId) {
        return  createNewNo("",unitId,"data_crkyw_jyxx_default","jydh",3);
    }
    @Override
    public String getBqDh(String unitId) {
//        return  createNewNo("",unitId,"data_crkyw_cplbjdy_default","bjbh",3);
            String newvalue = "";
            try {
                String prefix  = "";
                String tableName =  "data_crkyw_cplbjdy_default";
                String fieldName =  "bjbh";
                int NoLen = 2;
                String today = DateUtil.format(new Date(),"yyMMdd"); //得到当年日期
                QueryWrapper<Billnonew> queryWrapper = new QueryWrapper<>();
                queryWrapper.select("max(BillNo) as BillNo");
                queryWrapper.eq("unitId", unitId);
                queryWrapper.eq("TABLE_NAME", tableName);
                queryWrapper.eq("FIELD_NAME", fieldName);
                queryWrapper.likeRight("BillNo", prefix+today); //查询当天
                Billnonew billnonew = super.getOne(queryWrapper,false);
                String bilNo = "";
                if (billnonew != null) {
                    bilNo = billnonew.getBillNo();
                }
                newvalue = StringUtils.isBlank(bilNo) ?  prefix+ today + getNo("", NoLen) : getNo(bilNo, NoLen);
                //保存对象
                billnonew = new Billnonew();
                billnonew.setBillNo(newvalue);
                billnonew.setUnitId(unitId);
                billnonew.setTableName(tableName);
                billnonew.setFieldName(fieldName);
                if (!super.saveOrUpdate(billnonew)) {
                    newvalue = "";
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            return newvalue;
    }
    @Override
    public String getLyDh(String unitId) {
        return  createNewNo("",unitId,"ollection_review","collection_approval_no",3);
    }

    @Override
    public String getCfdh(String unitId) {
        return  createNewNo("",unitId,"commodity_splitting","plit_doc_no",3);
    }

    @Override
    public String getcplkctzdh(String unitId) {
        return  createNewNo("",unitId,"nventory_adjustment","nventory_adjustment_number",3);

    }
    @Override
    public String getFjDh(String unitId) {
        return  createNewNo("FJD",unitId,"data_crkyw_cplfj_default","fjdh",3);

    }

    @Override
    public String getScpcbySp(String unitId, String spbh, String scrq,String scpc) {
            String date = scrq;
            String bhandrq = StringUtils.substringBeforeLast(scpc, "_");
            String gys = StringUtils.substringAfterLast(scpc, "_");
            if(StringUtils.isNotBlank(scrq)){
               date =  DateUtil.format(DateUtil.parse(scrq,"yyyy-MM-dd"),"yyyyMMdd");
            }else{
                date = StringUtils.substring(bhandrq, bhandrq.length() - 8, bhandrq.length());

            }
        return spbh+date+"_"+gys;
    }

    @Override
    public String getCpljsBizNo(String unitId) {
        return createNewNo("",unitId,"settlement_management","js",3);
    }

    @Override
    public String getQckJydh(String unitId) {
            return  createNewNo("",unitId,"data_crkyw_ckzljc_default","zjdh",3);
    }

    @Override
    public String getTzdh(String unitId, Integer zylx) {
            String prefix = "LYDR"; //默认粮油调入
        if (zylx == 2){ //粮油出库
            prefix = "LYCK";
        }
        if (zylx == 3){ //专项入库
            prefix = "ZXDR";
        }
        if (zylx == 4) { //专项出库
            prefix = "ZXCK";
        }
            return createNewNo(prefix,unitId,"data_crkyw_tzdxx_default","tzdbm",4);
    }
}
