package com.bonus.sgzb.material.service.impl;

import com.bonus.sgzb.common.core.utils.DateUtils;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.material.domain.AgreementInfo;
import com.bonus.sgzb.material.mapper.AgreementInfoMapper;
import com.bonus.sgzb.material.service.AgreementInfoService;
import com.bonus.sgzb.material.vo.GlobalContants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author lsun
 */
@Service
public class AgreementInfoServiceImpl implements AgreementInfoService {

    @Autowired
    private AgreementInfoMapper agreementInfoMapper;

    @Override
    public List<AgreementInfo> getAgreementInfoAll(AgreementInfo bean) {
        return agreementInfoMapper.getAgreementInfoAll(bean);
    }

    @Override
    public List<AgreementInfo> getUnitList() {
        return agreementInfoMapper.getUnitList();
    }

    @Override
    public List<AgreementInfo> getProjectList() {
        return agreementInfoMapper.getProjectList();
    }

    @Override
    public int add(AgreementInfo bean) {
        List<AgreementInfo> list = agreementInfoMapper.getInfo(bean);
        int i = 0;
        if (list.size() > 0) {
            i = -1;
        } else {
            String agreementCode = purchaseCodeRule();
            bean.setAgreementCode(agreementCode);
            i = agreementInfoMapper.add(bean);
        }
        return i;
    }

    @Override
    public int update(AgreementInfo bean) {
        return agreementInfoMapper.update(bean);
    }

    @Override
    public AjaxResult deleteByIds(AgreementInfo bean) {
        if (bean.getAgreementId() != null) {
            //查询该协议是否存在领料任务，有的话不能删除
            int res = agreementInfoMapper.selectByagreementId(bean.getAgreementId());
            if (res > 0) {
                return AjaxResult.error("该协议已产生领料数据，不可删除");
            } else {
                int re = agreementInfoMapper.deleteByIds(bean);
                if (re > 0) {
                    return AjaxResult.success("操作成功");
                } else {
                    return AjaxResult.error("操作失败");
                }
            }
        } else {
            return AjaxResult.error("协议数据为空");
        }
    }

    @Override
    public AgreementInfo getAgreementInfoId(AgreementInfo bean) {
        return agreementInfoMapper.getAgreementInfoId(bean);
    }

    @Override
    public List<AgreementInfo> exportList(AgreementInfo agreementInfo) {
        return agreementInfoMapper.getAgreementInfoAll(agreementInfo);
    }

    @Override
    public List<AgreementInfo> getAllAgreementId() {
        return agreementInfoMapper.getAllAgreementId();
    }


    private String purchaseCodeRule() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date nowDate = DateUtils.getNowDate();
        String format = dateFormat.format(nowDate);
        String result = format.replace("-", "");
        int num = agreementInfoMapper.selectNumByMonth(nowDate);
        num = num + 1;
        String code = "";
        if (num > GlobalContants.NUM1 && num < GlobalContants.NUM2) {
            code = "XY" + result + "-00" + num;
        } else if (num > GlobalContants.NUM3 && num < GlobalContants.NUM4) {
            code = "XY" + result + "-0" + num;
        } else {
            code = "XY" + result + "-000" + num;
        }
        return code;
    }
}
