package co.dc.aptiv.service;

import co.dc.aptiv.AptivApp;
import co.dc.commons.basedao.CommonBaseDao;
import co.dc.commons.utils.DateTimeUtil;
import co.dc.aptiv.pojo.MaterielPojo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @describe: 物料
 * @author: liuxinglong
 * @version: 2018/4/17 15:21
 */
@Service("materielService")
public class MaterielService {

    private static final Log log = LogFactory.getLog(MaterielService.class);

    @Resource(name = "commonBaseDao")
    private CommonBaseDao baseDao;

    @Resource
    private QueryRunner queryRunner;

    /*
     * 1，增加=saveSku
     * 2，删除=delbyid
     * 3，修改=updateSku
     * 4，查询=findbyid
     * 5，查询列表=listAll
     * 6，查询是否有sku已经存在=isExistByproductCode
     */

    /**
     * 增加物料
     *
     * @return
     */
    public long saveMateriel(MaterielPojo materielPojo) {
        if (materielPojo != null) {
            log.info("添加一个物料 dpn=" + materielPojo.getDpn() + " cpn=" + materielPojo.getCpn() + " type=" + materielPojo.getType());
            return baseDao.save(materielPojo);
        } else {
            return 0;
        }
    }

    /**
     * 根据ID删除物料
     *
     * @param id
     * @return
     */
    public int deleteById(long id) {
        log.info("删除一个物料  id=" + id);
        if (id != 0) {
            return baseDao.del(MaterielPojo.class, id);
        } else {
            return 0;
        }
    }


    /**
     * 根据dpn删除物料
     *
     * @param dpn
     * @return
     */
    public int deleteByDpn(String dpn) {
        log.info("根据dpn删除物料 dpn=" + dpn);
        if (!StringUtils.isEmpty(dpn)) {
            return baseDao.del(MaterielPojo.class, "dpn", dpn);
        } else {
            return 0;
        }
    }

    /**
     * 更新物料序号
     *
     * @param id
     * @return
     */
    public int updateMaterielpojoMaxNum(long id) {
        log.info("更新物料序号 id=" + id);
        MaterielPojo materielPojo = AptivApp.materielService.findById(id);
        materielPojo.setMaxNum(materielPojo.getMaxNum() + 1);
        materielPojo.setModifyTime(DateTimeUtil.getDateTimeString());
        return baseDao.update(materielPojo);
    }

    /**
     * 更新物料
     *
     * @param materielPojo
     * @return
     */
    public int updateMaterielpojo(MaterielPojo materielPojo) {
        log.info("更新物料序号 物料名称=" + materielPojo.getDpn());
        return baseDao.update(materielPojo);
    }

    /**
     * 查询单个物料
     *
     * @param id
     * @return
     */
    public MaterielPojo findById(Long id) {
        MaterielPojo materielPojo = baseDao.findById(MaterielPojo.class, id);
        if (materielPojo != null) {
            return materielPojo;
        } else {
            return new MaterielPojo();
        }
    }

    /**
     * 根据dpn查物料
     *
     * @param dpn
     * @return
     */
    public MaterielPojo findByDPN(String dpn) {
        MaterielPojo materielPojo = baseDao.findByParam(MaterielPojo.class, "dpn", dpn);
        if (materielPojo != null) {
            return materielPojo;
        }
        return null;
    }

    /**
     * 根据cpn查物料
     *
     * @param dpn
     * @param cpn
     * @return
     */
    public MaterielPojo findByCPN(String dpn, String cpn) {
        try {
            MaterielPojo materielPojo = baseDao.query("SELECT * FROM materiel WHERE delFlag = 1 AND （dpn = ? OR cpn = ?）", new BeanHandler<MaterielPojo>(MaterielPojo.class), dpn, cpn);
            if (materielPojo != null) {
                return materielPojo;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询所有物料数据
     *
     * @return
     */
    public List<MaterielPojo> listAll() {
        log.info("查询所有物料数据。");
        List<MaterielPojo> materielPojos = baseDao.listByParam(MaterielPojo.class, "1", "1", "id asc");
        if (materielPojos != null) {
            return materielPojos;
        } else {
            return new ArrayList<MaterielPojo>();
        }
    }

    /**
     * 查询是否有sku已经存在=isExistByproductCode
     *
     * @return
     */
    public boolean isExistByproductCode(String productCode, long skuId) {
        log.info("查询是否有sku已经存在。");
        List<MaterielPojo> materielPojos = baseDao.listByParam(MaterielPojo.class, "productCode", productCode);
        boolean reBoolean = false;
        if (materielPojos != null && materielPojos.size() > 0) {
            if (skuId == 0) {
                reBoolean = true;
            } else {
                for (MaterielPojo materielPojo : materielPojos) {
                    if (materielPojo.getId() != skuId) {
                        reBoolean = true;
                        break;
                    }
                }
            }
        } else {
            reBoolean = false;
        }
        return reBoolean;
    }


    /**
     * 判断参数是不是符合要求
     *
     * @param paramName  要查询的字段名
     * @param paramValue 要查询的参数
     * @param MaxLength  参数的最大长度
     * @param id         0 添加 >0修改
     * @return
     */
    public int isExistByParam(String paramName, String paramValue, int MaxLength, long id) {
        String sql = "SELECT * FROM materiel WHERE delFlag = 0 AND ? = ?";
        if (id > 0) {
            sql += " AND id != " + id;
        }
        try {
            List<MaterielPojo> materielPojos = baseDao.query(sql, new BeanListHandler<MaterielPojo>(MaterielPojo.class), paramName, paramValue);
            if (materielPojos != null && materielPojos.size() > 0) {
                return 1;
            }
            if (MaxLength != paramValue.length()) {
                return 2;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


}
