package com.ruoyi.warehouseBusiness.service.impl;

import com.ruoyi.basicData.domain.TCorps;
import com.ruoyi.basicData.mapper.TCorpsMapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.shipping.domain.TAddress;
import com.ruoyi.shipping.domain.TVoyage;
import com.ruoyi.shipping.mapper.TAddressMapper;
import com.ruoyi.shipping.mapper.TVoyageMapper;
import com.ruoyi.warehouseBusiness.domain.BillnoDel;
import com.ruoyi.warehouseBusiness.domain.BillnoSerial;
import com.ruoyi.warehouseBusiness.mapper.BillnoDelMapper;
import com.ruoyi.warehouseBusiness.mapper.BillnoSerialMapper;
import com.ruoyi.warehouseBusiness.service.IBillnoSerialService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 客户存货编号流水号Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-23
 */
@Service
public class BillnoSerialServiceImpl implements IBillnoSerialService {
    @Autowired
    private TCorpsMapper tCorpsMapper;

    @Autowired
    private BillnoDelMapper billnoDelMapper;

    @Autowired
    private BillnoSerialMapper billnoSerialMapper;
    @Autowired
    private TAddressMapper tAddressMapper;
    @Autowired
    private TVoyageMapper tVoyageMapper;

    /**
     * 获取业务编号
     *
     * @param billType 编号类型(SJRK（入库） SJCK（实际出库） CKDB（调拨） HQZY（货权转移）CDZZ（场地直装）)
     * @param time     入库时间
     * @return 结果
     */
    public String getBillNo(String billType, Date time) {
        String billNo = null;
        BillnoDel billnoDel = new BillnoDel();
        billnoDel.setBillType(billType);
        List<BillnoDel> billnoDels = billnoDelMapper.selectBillnoDelList(billnoDel);
        // 如果有数据在删除列表
        if (StringUtils.isNotEmpty(billnoDels)) {
            billnoDelMapper.deleteBillnoDelById(billnoDels.get(0).getId());
            billNo = billnoDels.get(0).getBillNo();
            return billNo;
        } else {
            String yyyyMM = DateFormatUtils.format(time, "yyyyMM");
            BillnoSerial billnoSerial = new BillnoSerial();
            billnoSerial.setBillType(billType);
            billnoSerial.setBillPrefix(yyyyMM);
            BillnoSerial billnoSerials = billnoSerialMapper.selectBillnoSerial(billnoSerial);
            if (StringUtils.isNotNull(billnoSerials)) {
                billnoSerials.setSerial(billnoSerials.getSerial() + 1);
                billnoSerialMapper.updateBillnoSerial(billnoSerials);
                if (billnoSerials.getSerial() <= 9) {
                    billNo = yyyyMM + "00" + billnoSerials.getSerial();
                } else if (billnoSerials.getSerial() <= 99) {
                    billNo = yyyyMM + "0" + billnoSerials.getSerial();
                } else if (billnoSerials.getSerial() <= 999) {
                    billNo = yyyyMM + "" + billnoSerials.getSerial();
                }
            } else {
                billNo = yyyyMM + "001";
                billnoSerial.setSerial(1);
                billnoSerialMapper.insertBillnoSerial(billnoSerial);
            }
        }
        if ("SJRK".equals(billType)) {
            billNo = "RK" + billNo;
        } else if ("SJCK".equals(billType)) {
            billNo = "CK" + billNo;
        } else if ("CKDB".equals(billType)) {
            billNo = "DB" + billNo;
        } else if ("HQZY".equals(billType)) {
            billNo = "HZ" + billNo;
        } else if ("WA".equals(billType)) {
            billNo = "WA" + billNo;
        } else if ("DZ".equals(billType)) {
            billNo = "DZ" + billNo;
        } else if ("SF".equals(billType)) {
            billNo = "SF" + billNo;
        } else if ("FF".equals(billType)) {
            billNo = "FF" + billNo;
        } else if ("SFDZ".equals(billType)) {
            billNo = "SFDZ" + billNo;
        } else if ("JSSF".equals(billType)) {
            billNo = "JSSF" + billNo;
        } else if ("JSFF".equals(billType)) {
            billNo = "JSFF" + billNo;
        } else if ("HWTG".equals(billType)) {
            billNo = "HWTG" + billNo;
        }else if ("SE".equals(billType)){
            billNo = "SE" + billNo;
        }else if ("KHDZ".equals(billType)) {
            billNo = "KHDZ" + billNo;
        } else if ("KHSF".equals(billType)) {
            billNo = "KHSF" + billNo;
        } else if ("KHFF".equals(billType)) {
            billNo = "KHFF" + billNo;
        }else if ("ApplyFP".equals(billType)) {
            billNo = "ApplyFP" + billNo;
        }else if ("invoiceFP".equals(billType)) {
            billNo = "invoiceFP" + billNo;
        }else if ("INV".equals(billType)) {
            billNo = "INV" + billNo;
        }else if ("CDZZ".equals(billType)) {
            billNo = "ZZ" + billNo;
        }
        return billNo;
    }

    /**
     * 获取客户存货编号
     *
     * @param corpId 客户id
     * @param time   时间
     * @return 结果
     */
    public String getCorpNo(long corpId, Date time) {
        String corpNo = null;
        String yyyy = DateFormatUtils.format(time, "yyyy");
        TCorps corps = tCorpsMapper.selectTCorpsById(corpId);
        corpNo = corps.getfNo() + yyyy;
        BillnoSerial billnoSerial = new BillnoSerial();
        billnoSerial.setBillType("corpNo");
        billnoSerial.setBillPrefix(yyyy);
        BillnoSerial billnoSerials = billnoSerialMapper.selectBillnoSerial(billnoSerial);
        if (StringUtils.isNotNull(billnoSerials)) {
            billnoSerials.setSerial(billnoSerials.getSerial() + 1);
            billnoSerialMapper.updateBillnoSerial(billnoSerials);
            if (billnoSerials.getSerial() <= 9) {
                corpNo = corpNo + "000" + billnoSerials.getSerial();
            } else if (billnoSerials.getSerial() <= 99) {
                corpNo = corpNo + "00" + billnoSerials.getSerial();
            } else if (billnoSerials.getSerial() <= 999) {
                corpNo = corpNo + "0" + billnoSerials.getSerial();
            } else if (billnoSerials.getSerial() <= 9999) {
                corpNo = corpNo + billnoSerials.getSerial();
            }
        } else {
            billnoSerial.setSerial(1);
            billnoSerialMapper.insertBillnoSerial(billnoSerial);
            corpNo = corpNo + "0001";
        }
        return corpNo;
    }

    /**
     * 获取入库 出库 流水号
     *
     * @param billType 编号类型(RK（入库） CK（出库） )
     * @param time     入库时间
     * @return 结果
     */
    public String getSerialNumber(String billType, Date time) {
        String billNo = null;
        String yyMMdd = null;
        if ("JH".equals(billType) || "JHBF".equals(billType) || "DDBF".equals(billType) || "JHBGBF".equals(billType)) {
            yyMMdd = DateFormatUtils.format(time, "yyyyMMdd");
        } else if ("CCF".equals(billType) || "SJLSH".equals(billType)) {
            yyMMdd = DateFormatUtils.format(time, "yyMM");
        } else {
            yyMMdd = DateFormatUtils.format(time, "yyMMdd");
        }
        if ("RK".equals(billType) || "CK".equals(billType) || "DB".equals(billType)) {
            // 流水号 是否有回收
            BillnoDel billnoDel = new BillnoDel();
            billnoDel.setBillType(billType);
            List<BillnoDel> billnoDels = billnoDelMapper.selectBillnoDelList(billnoDel);
            // 如果有数据在删除列表
            if (StringUtils.isNotEmpty(billnoDels)) {
                billnoDelMapper.deleteBillnoDelById(billnoDels.get(0).getId());
                billNo = billnoDels.get(0).getBillNo();
                return billNo;
            }
        }
        boolean tms = false;
        if ("SJLSH".equals(billType)) {
            tms = true;
        }
        BillnoSerial billnoSerial = new BillnoSerial();
        billnoSerial.setBillType(billType);
        billnoSerial.setBillPrefix(yyMMdd);
        BillnoSerial billnoSerials = billnoSerialMapper.selectBillnoSerial(billnoSerial);
        if (StringUtils.isNotNull(billnoSerials)) {
            if (tms) {
                if (billnoSerials.getSerial() <= 9) {
                    billNo = yyMMdd + "000" + billnoSerials.getSerial();
                } else if (billnoSerials.getSerial() <= 99) {
                    billNo = yyMMdd + "00" + billnoSerials.getSerial();
                } else if (billnoSerials.getSerial() <= 999) {
                    billNo = yyMMdd + "0" + billnoSerials.getSerial();
                } else if (billnoSerials.getSerial() <= 9999) {
                    billNo = yyMMdd + "" + billnoSerials.getSerial();
                }
                billnoSerials.setSerial(billnoSerials.getSerial() + 1);
                billnoSerialMapper.updateBillnoSerial(billnoSerials);
                return billNo;
            }
            if (billnoSerials.getSerial() <= 9) {
                billNo = yyMMdd + "00" + billnoSerials.getSerial();
            } else if (billnoSerials.getSerial() <= 99) {
                billNo = yyMMdd + "0" + billnoSerials.getSerial();
            } else if (billnoSerials.getSerial() <= 999) {
                billNo = yyMMdd + "" + billnoSerials.getSerial();
            }
            billnoSerials.setSerial(billnoSerials.getSerial() + 1);
            billnoSerialMapper.updateBillnoSerial(billnoSerials);
        } else {
            billnoSerial.setSerial(2);
            if (tms) {
                billNo = yyMMdd + "0001";
                billnoSerialMapper.insertBillnoSerial(billnoSerial);
                return billNo;
            }
            billNo = yyMMdd + "001";
            billnoSerialMapper.insertBillnoSerial(billnoSerial);
        }
        billNo = billType + billNo;
        return billNo;
    }

    /**
     * 获取入库 出库 流水号
     *
     * @param billType 编号类型(RK（入库） CK（出库） )
     * @param time     入库时间
     * @return 结果
     */
    public String getFictitiousNumber(String billType, Date time) {
        String billNo = null;
        String yyMMdd = DateFormatUtils.format(time, "yyMMdd");
        BillnoSerial billnoSerial = new BillnoSerial();
        billnoSerial.setBillType(billType);
        billnoSerial.setBillPrefix(yyMMdd);
        BillnoSerial billnoSerials = billnoSerialMapper.selectBillnoSerial(billnoSerial);
        if (StringUtils.isNotNull(billnoSerials)) {
            if (billnoSerials.getSerial() <= 9) {
                billNo = yyMMdd + "00" + billnoSerials.getSerial();
            } else if (billnoSerials.getSerial() <= 99) {
                billNo = yyMMdd + "0" + billnoSerials.getSerial();
            } else if (billnoSerials.getSerial() <= 999) {
                billNo = yyMMdd + "" + billnoSerials.getSerial();
            }
        } else {
            billNo = yyMMdd + "001";
        }
        billNo = billType + billNo;
        return billNo;
    }

    /**
     * 查询客户存货编号流水号
     *
     * @param id 客户存货编号流水号ID
     * @return 客户存货编号流水号
     */
    @Override
    public BillnoSerial selectBillnoSerialById(Long id) {
        return billnoSerialMapper.selectBillnoSerialById(id);
    }

    /**
     * 查询客户存货编号流水号列表
     *
     * @param billnoSerial 客户存货编号流水号
     * @return 客户存货编号流水号
     */
    @Override
    public List<BillnoSerial> selectBillnoSerialList(BillnoSerial billnoSerial) {
        return billnoSerialMapper.selectBillnoSerialList(billnoSerial);
    }

    /**
     * 新增客户存货编号流水号
     *
     * @param billnoSerial 客户存货编号流水号
     * @return 结果
     */
    @Override
    public int insertBillnoSerial(BillnoSerial billnoSerial) {
        return billnoSerialMapper.insertBillnoSerial(billnoSerial);
    }

    /**
     * 修改客户存货编号流水号
     *
     * @param billnoSerial 客户存货编号流水号
     * @return 结果
     */
    @Override
    public int updateBillnoSerial(BillnoSerial billnoSerial) {
        return billnoSerialMapper.updateBillnoSerial(billnoSerial);
    }

    /**
     * 批量删除客户存货编号流水号
     *
     * @param ids 需要删除的客户存货编号流水号ID
     * @return 结果
     */
    @Override
    public int deleteBillnoSerialByIds(Long[] ids) {
        return billnoSerialMapper.deleteBillnoSerialByIds(ids);
    }

    /**
     * 删除客户存货编号流水号信息
     *
     * @param id 客户存货编号流水号ID
     * @return 结果
     */
    @Override
    public int deleteBillnoSerialById(Long id) {
        return billnoSerialMapper.deleteBillnoSerialById(id);
    }

    /**
     * 凯和获取提单号
     * @param billType 类型默认KH
     * @param fLoadPortId 起运港
     * @param fDestPortId 目的港
     * @param fVoyId 航次信息
     * @return
     */
    public String getMblno(String billType, Long fLoadPortId,Long fDestPortId,Long fVoyId) {
        //获取起运港简称
        TAddress address = tAddressMapper.selectTAddressById(fLoadPortId);
        if (StringUtils.isNotEmpty(address.getfNo())){
            billType = billType+address.getfNo();
        }
        //获取目的港简称
        TAddress tAddress = tAddressMapper.selectTAddressById(fDestPortId);
        if (StringUtils.isNotEmpty(tAddress.getfNo())){
            billType = billType+tAddress.getfNo();
        }
        //查询航次信息
        TVoyage tVoyage = tVoyageMapper.selectTVoyageById(fVoyId);
        String voyage = tVoyage.getfNo();
        String Mblno = null;
        /*BillnoDel billnoDel = new BillnoDel();
        billnoDel.setBillType(billType);
        List<BillnoDel> billnoDels = billnoDelMapper.selectBillnoDelList(billnoDel);
        // 如果有数据在删除列表
        if (StringUtils.isNotEmpty(billnoDels)) {
            billnoDelMapper.deleteBillnoDelById(billnoDels.get(0).getId());
            Mblno = billnoDels.get(0).getBillNo();
            return Mblno;
        } else {*/
            BillnoSerial billnoSerial = new BillnoSerial();
            billnoSerial.setBillType(billType);
            billnoSerial.setBillPrefix(voyage);
            BillnoSerial billnoSerials = billnoSerialMapper.selectBillnoSerial(billnoSerial);
            if (StringUtils.isNotNull(billnoSerials)) {
                billnoSerials.setSerial(billnoSerials.getSerial() + 1);
                billnoSerialMapper.updateBillnoSerial(billnoSerials);
                if (billnoSerials.getSerial() <= 9) {
                    Mblno = voyage + "00" + billnoSerials.getSerial();
                } else if (billnoSerials.getSerial() <= 99) {
                    Mblno = voyage + "0" + billnoSerials.getSerial();
                } else if (billnoSerials.getSerial() <= 999) {
                    Mblno = voyage + "" + billnoSerials.getSerial();
                }
            } else {
                Mblno = voyage + "001";
                billnoSerial.setSerial(1);
                billnoSerialMapper.insertBillnoSerial(billnoSerial);
            }
        /*}*/
            Mblno = billType + Mblno;
        return Mblno;
    }
}
