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.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;

    /**
     * 获取业务编号
     *
     * @param billType 编号类型(SJRK（入库） SJCK（实际出库） CKDB（调拨） HQZY（货权转移）)
     * @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;
        }
        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)) {
            yyMMdd = DateFormatUtils.format(time, "yyMM");
        } else {
            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();
            }
            billnoSerials.setSerial(billnoSerials.getSerial() + 1);
            billnoSerialMapper.updateBillnoSerial(billnoSerials);
        } else {
            billNo = yyMMdd + "001";
            billnoSerial.setSerial(2);
            billnoSerialMapper.insertBillnoSerial(billnoSerial);
        }
        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);
    }
}
