package market.Lms.Warehouse.impl;

import java.util.HashMap;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Warehouse.mapper.YdmBaseButtressconfigMapper;
import market.Lms.Warehouse.mapper.YdmProductDetailMapper;
import market.Lms.Warehouse.service.YdmProductMachineService;
import market.Lms.Warehouse.vo.YdmBaseButtressconfig;
import market.Lms.Warehouse.vo.YdmProductDetail;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;

/**
 * @author Ljy
 * @description
 * @date 2020/4/15
 */
@RemoteService(path = "YdmProductMachineService")
public class YdmProductMachineServiceImpl extends BaseServiceImpl<YdmBaseButtressconfig, Object> implements YdmProductMachineService {
    /**
     * 功能描述: 根据卷号获取区域信息
     *
     * @param cp
     * @return market.Com.vo.ResultModel
     * @author Ljy
     * @date 2020/4/15
     */
    public ResultModel getAreaByYdmInfo(CommonPage<YdmProductDetail> cp) {
        ResultModel rm = new ResultModel("200", "查询成功！");
        if (cp.getObject() == null || StringUtils.isBlank(cp.getObject().getCoilno())) {
            rm.setState("500");
            rm.setMsgInfo("卷号不能为空！");
            return rm;
        }
        String coilno = cp.getObject().getCoilno();
        YdmProductDetail ypd = this.getMapper(YdmProductDetailMapper.class).findById(coilno);
        if (ypd == null) {
            rm.setState("500");
            rm.setMsgInfo("卷号【" + coilno + "】库存记录不存在！");
            return rm;
        } else if (StringUtils.isBlank(ypd.getStorageattr())) {
            rm.setState("500");
            rm.setMsgInfo("卷号【" + coilno + "】库存记录未维护仓库编码！");
            return rm;
        }
        String storageattr = ypd.getStorageattr();//仓库编码
        List<YdmBaseButtressconfig> ybbList = this.getMapper(YdmBaseButtressconfigMapper.class).findWithCondition(null, " and storageattr = '" + storageattr + "' and storagesort = 'A'", null, 0, 0);
        if (CollectionUtils.isEmpty(ybbList)) {
            rm.setState("500");
            rm.setMsgInfo("垛位记录中未维护仓库编码【" + storageattr + "】的仓库信息！");
            return rm;
        }
        List<YdmBaseButtressconfig> ybbList1 = this.getMapper(YdmBaseButtressconfigMapper.class).doQueryButtressBySortcode(ybbList.get(0).getButtressno(), "B");
        rm.setData(ybbList1);
        return rm;
    }

    /**
     * 功能描述: 根据区域编码获取垛位信息
     *
     * @param sortcode
     * @return market.Com.vo.ResultModel
     * @author Ljy
     * @date 2020/4/15
     */
    public ResultModel queryButtressByAreaNo(String sortcode) {
        ResultModel rm = new ResultModel("200", "查询成功！");
        if (StringUtils.isBlank(sortcode)) {
            rm.setState("500");
            rm.setMsgInfo("区域编码不能为空！");
            return rm;
        }
        List<YdmBaseButtressconfig> ybbList = this.getMapper(YdmBaseButtressconfigMapper.class).doQueryButtressBySortcode(sortcode, "C");
        if (CollectionUtils.isEmpty(ybbList)) {
            rm.setState("500");
            rm.setMsgInfo("请联系管理员，检查区域编码【" + sortcode + "】是否正确,或区域未维护垛位基础信息！");
            return rm;
        }
        for (YdmBaseButtressconfig ybb :
                ybbList) {
            Double colwidth = ybb.getColwidth();
            HashMap<String, Integer> localityMap = new HashMap<String, Integer>();
            if (colwidth > 0) {
                for (int i = 1; i <= colwidth; i++) {
                    localityMap.put(i+"", 0);
                }
            } else {
                localityMap = null;
            }
            ybb.setLocalityMap(localityMap);
            List<YdmBaseButtressconfig> ybbList1 = this.getMapper(YdmBaseButtressconfigMapper.class).queryButtressInfoByButtressno(ybb.getButtressno());
            if (CollectionUtils.isNotEmpty(ybbList1)) {
                for (YdmBaseButtressconfig ybb1 :
                        ybbList1) {
                    ybb.getLocalityMap().put(ybb1.getLocality(), Integer.parseInt(ybb1.getCoilnum()));
                }
            }
        }
        rm.setData(ybbList);
        return rm;
    }

    /**
     * 功能描述: 根据垛位编码获取垛位信息
     *
     * @param cp
     * @return market.Com.vo.ResultModel
     * @author Ljy
     * @date 2020/4/15
     */
    public ResultModel queryKCInfoByButtressInfo(CommonPage<YdmBaseButtressconfig> cp) {
        ResultModel rm = new ResultModel("200", "查询成功！");
        YdmBaseButtressconfig ybb = cp.getObject() == null ? new YdmBaseButtressconfig() : cp.getObject();
        List<YdmProductDetail> ypdList = this.getMapper(YdmBaseButtressconfigMapper.class).queryKCInfoByButtressInfo(ybb);
        rm.setData(ypdList);
        return rm;
    }

    /**
     * 功能描述: 根据垛位编码获取垛位信息,位置上库存的数量
     *
     * @param buttressno
     * @return market.Com.vo.ResultModel
     * @author Ljy
     * @date 2020/4/15
     */
    public ResultModel queryButtressInfoByButtressno(String buttressno) {
        ResultModel rm = new ResultModel("200", "查询成功！");
        if (StringUtils.isBlank(buttressno)) {
            rm.setState("500");
            rm.setMsgInfo("垛位编码不能为空！");
            return rm;
        }
        List<YdmBaseButtressconfig> ybbList = this.getMapper(YdmBaseButtressconfigMapper.class).queryButtressInfoByButtressno(buttressno);
        rm.setData(ybbList);
        return rm;
    }
}
