package com.jd.jdx.x.sr.cms.service.container.impl;


import com.jd.bk.common.web.result.Result;
import com.jd.jdx.x.sr.cms.domain.base.constants.BaseConstants;
import com.jd.jdx.x.sr.cms.domain.base.ui.Page;
import com.jd.jdx.x.sr.cms.domain.biz.dto.BizSequenceDto;
import com.jd.jdx.x.sr.cms.domain.container.dto.GoodsBoxDto;
import com.jd.jdx.x.sr.cms.domain.container.dto.StaticTemGoodsDto;
import com.jd.jdx.x.sr.cms.domain.container.model.*;
import com.jd.jdx.x.sr.cms.domain.container.query.ConSellerGoodsQuery;
import com.jd.jdx.x.sr.cms.domain.container.query.ConStaticTemplateQuery;
import com.jd.jdx.x.sr.cms.domain.container.query.ConTemGoodsQuery;
import com.jd.jdx.x.sr.cms.domain.util.CommonConfig;
import com.jd.jdx.x.sr.cms.manager.biz.BizSequenceManager;
import com.jd.jdx.x.sr.cms.manager.container.*;
import com.jd.jdx.x.sr.cms.service.container.ConStaticTemplateService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 业务模块-商品静态模板服务实现
 *
 * @author wanghonghong5
 */
@Component("conStaticTemplateService")
public class ConStaticTemplateServiceImpl implements ConStaticTemplateService {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(ConStaticTemplateServiceImpl.class);
    /**
     * the ConStaticTemplateManager
     */
    private ConStaticTemplateManager conStaticTemplateManager;

    private ConTemGoodsManager conTemGoodsManager;

    private BizSequenceManager bizSequenceManager;

    private ConDeviceGoodsManager conDeviceGoodsManager;

    private ConDeviceManager conDeviceManager;

    /**
     * 商家商品
     */
    private ConSellerGoodsManager conSellerGoodsManager;

    @Override
    public Result insertConStaticTemplate(ConStaticTemplate conStaticTemplate) {
        Result result = new Result();
        try {
            if (null != conStaticTemplate) {
                boolean returnFlag = true;

                //保存通用消息
                returnFlag = conStaticTemplateManager.insertConStaticTemplate(conStaticTemplate);
                if (returnFlag) {
                    result.setSuccess(true);
                    result.setResultCode(BaseConstants.SERVICE_RESULT_SUCCESS_CODE);
                    result.addDefaultModel(BaseConstants.SERVICE_RESULT_SUCCESS_CODE,
                            returnFlag);
                } else {
                    result.setSuccess(false);
                    result.setResultCode(BaseConstants.SERVICE_RESULT_FAIL_CODE);
                    result.addDefaultModel(BaseConstants.SERVICE_RESULT_FAIL_CODE,
                            BaseConstants.SERVICE_RESULT_FAIL_MESSAGE);
                }
            } else {
                result.setSuccess(false);
                result.setResultCode(BaseConstants.SERVICE_RESULT_FAIL_CODE);
                result.addDefaultModel(BaseConstants.SERVICE_RESULT_FAIL_CODE,
                        BaseConstants.SERVICE_RESULT_FAIL_MESSAGE);
            }
        } catch (Exception e) {
            logger.error("ConStaticTemplateServiceImpl!insertConStaticTemplate -> error!!", e);
            result.setSuccess(false);
        }
        return result;
    }

    @Override
    public Result updateConStaticTemplate(ConStaticTemplate conStaticTemplate) {
        Result result = new Result();
        try {
            if (null != conStaticTemplate) {
                boolean returnFlag = true;

                //保存通用消息
                returnFlag = conStaticTemplateManager.updateConStaticTemplate(conStaticTemplate);
                if (returnFlag) {
                    result.setSuccess(true);
                    result.setResultCode(BaseConstants.SERVICE_RESULT_SUCCESS_CODE);
                    result.addDefaultModel(BaseConstants.SERVICE_RESULT_SUCCESS_CODE,
                            returnFlag);
                } else {
                    result.setSuccess(false);
                    result.setResultCode(BaseConstants.SERVICE_RESULT_FAIL_CODE);
                    result.addDefaultModel(BaseConstants.SERVICE_RESULT_FAIL_CODE,
                            BaseConstants.SERVICE_RESULT_FAIL_MESSAGE);
                }
            } else {
                result.setSuccess(false);
                result.setResultCode(BaseConstants.SERVICE_RESULT_FAIL_CODE);
                result.addDefaultModel(BaseConstants.SERVICE_RESULT_FAIL_CODE,
                        BaseConstants.SERVICE_RESULT_FAIL_MESSAGE);
            }
        } catch (Exception e) {
            logger.error("ConStaticTemplateServiceImpl!updateConStaticTemplate -> error!!", e);
            result.setSuccess(false);
        }
        return result;
    }

    @Override
    public Result deleteConStaticTemplate(Long id) {
        Result result = new Result();
        try {
            if (null != id) {
                boolean returnFlag = true;

                //保存通用消息
                returnFlag = conStaticTemplateManager.deleteConStaticTemplate(id);
                if (returnFlag) {
                    result.setSuccess(true);
                    result.setResultCode(BaseConstants.SERVICE_RESULT_SUCCESS_CODE);
                    result.addDefaultModel(BaseConstants.SERVICE_RESULT_SUCCESS_CODE,
                            returnFlag);
                } else {
                    result.setSuccess(false);
                    result.setResultCode(BaseConstants.SERVICE_RESULT_FAIL_CODE);
                    result.addDefaultModel(BaseConstants.SERVICE_RESULT_FAIL_CODE,
                            BaseConstants.SERVICE_RESULT_FAIL_MESSAGE);
                }
            } else {
                result.setSuccess(false);
                result.setResultCode(BaseConstants.SERVICE_RESULT_FAIL_CODE);
                result.addDefaultModel(BaseConstants.SERVICE_RESULT_FAIL_CODE,
                        BaseConstants.SERVICE_RESULT_FAIL_MESSAGE);
            }
        } catch (Exception e) {
            logger.error("ConStaticTemplateServiceImpl!deleteConStaticTemplate -> error!!", e);
            result.setSuccess(false);
        }
        return result;
    }

    @Override
    public Result getConStaticTemplate(Long id) {
        Result result = new Result();
        try {
            if (null != id) {
                boolean returnFlag = true;

                //保存通用消息
                ConStaticTemplate conStaticTemplate = conStaticTemplateManager.getConStaticTemplate(id);
                if (conStaticTemplate != null) {
                    result.setSuccess(true);
                    result.setResultCode(BaseConstants.SERVICE_RESULT_SUCCESS_CODE);
                    result.addDefaultModel(BaseConstants.SERVICE_RESULT_SUCCESS_CODE, conStaticTemplate);
                } else {
                    result.setSuccess(false);
                    result.setResultCode(BaseConstants.SERVICE_RESULT_FAIL_CODE);
                    result.addDefaultModel(BaseConstants.SERVICE_RESULT_FAIL_CODE,
                            BaseConstants.SERVICE_RESULT_FAIL_MESSAGE);
                }
            } else {
                result.setSuccess(false);
                result.setResultCode(BaseConstants.SERVICE_RESULT_FAIL_CODE);
                result.addDefaultModel(BaseConstants.SERVICE_RESULT_FAIL_CODE,
                        BaseConstants.SERVICE_RESULT_FAIL_MESSAGE);
            }
        } catch (Exception e) {
            logger.error("ConStaticTemplateServiceImpl!getConStaticTemplate -> error!!", e);
            result.setSuccess(false);
        }
        return result;
    }

    @Override
    public Result findConStaticTemplateList(Page page, ConStaticTemplateQuery conStaticTemplateQuery) {
        Result result = new Result();
        try {
            if (null != conStaticTemplateQuery) {
                boolean returnFlag = true;

                //保存通用消息
                conStaticTemplateQuery.setTemType(CommonConfig.temTypeByUser);
                conStaticTemplateManager.findConStaticTemplateList(page, conStaticTemplateQuery);
                if (conStaticTemplateQuery != null) {
                    result.setSuccess(true);
                    result.setResultCode(BaseConstants.SERVICE_RESULT_SUCCESS_CODE);
                    result.addDefaultModel(BaseConstants.SERVICE_RESULT_SUCCESS_CODE, page);
                } else {
                    result.setSuccess(false);
                    result.setResultCode(BaseConstants.SERVICE_RESULT_FAIL_CODE);
                    result.addDefaultModel(BaseConstants.SERVICE_RESULT_FAIL_CODE,
                            BaseConstants.SERVICE_RESULT_FAIL_MESSAGE);
                }
            } else {
                result.setSuccess(false);
                result.setResultCode(BaseConstants.SERVICE_RESULT_FAIL_CODE);
                result.addDefaultModel(BaseConstants.SERVICE_RESULT_FAIL_CODE,
                        BaseConstants.SERVICE_RESULT_FAIL_MESSAGE);
            }
        } catch (Exception e) {
            logger.error("ConStaticTemplateServiceImpl!findConStaticTemplateList -> error!!", e);
            result.setSuccess(false);
        }
        return result;
    }

    @Override
    public Result insertStaticTemAndGoods(StaticTemGoodsDto staticTemGoodsDto) {
        Result result = new Result();
        try {
            if (null != staticTemGoodsDto) {
                boolean returnFlag = true;
                // 生成模板编号
                BizSequenceDto bizSequenceDto = bizSequenceManager.getSequence(CommonConfig.TemNo_SEQ, 1);
                if (bizSequenceDto != null) {
                    String temNo = CommonConfig.Tem_Pre + bizSequenceDto.getEndSeq();
                    staticTemGoodsDto.setTemNo(temNo);
                    //保存通用消息
                    returnFlag = conStaticTemplateManager.insertStaticTemAndGoods(staticTemGoodsDto);
                    if (returnFlag) {
                        result.setSuccess(true);
                        result.setResultCode(BaseConstants.SERVICE_RESULT_SUCCESS_CODE);
                        result.addDefaultModel(BaseConstants.SERVICE_RESULT_SUCCESS_CODE,
                                returnFlag);
                    } else {
                        result.setSuccess(false);
                        result.setResultCode(BaseConstants.SERVICE_RESULT_FAIL_CODE);
                        result.addDefaultModel(BaseConstants.SERVICE_RESULT_FAIL_CODE,
                                BaseConstants.SERVICE_RESULT_FAIL_MESSAGE);
                    }
                } else {
                    result.setSuccess(false);
                    result.setResultCode(BaseConstants.SERVICE_RESULT_FAIL_CODE);
                    result.addDefaultModel(BaseConstants.SERVICE_RESULT_FAIL_CODE,
                            BaseConstants.SERVICE_RESULT_FAIL_MESSAGE);
                }
            } else {
                logger.error("没有取到序列号:" + staticTemGoodsDto.getTemName());
                result.setSuccess(false);
                result.setResultCode(BaseConstants.SERVICE_RESULT_FAIL_CODE);
                result.addDefaultModel(BaseConstants.SERVICE_RESULT_FAIL_CODE,
                        BaseConstants.SERVICE_RESULT_FAIL_MESSAGE);
            }
        } catch (Exception e) {
            logger.error("ConStaticTemplateServiceImpl!insertStaticTemAndGoods -> error!!", e);
            result.setSuccess(false);
        }
        return result;
    }

    @Override
    public Result updateStaticTemAndGoods(StaticTemGoodsDto staticTemGoodsDto) {
        Result result = new Result();
        try {
            if (null != staticTemGoodsDto) {
                boolean returnFlag = true;
                //保存通用消息
                ConStaticTemplate conStaticTemplate = conStaticTemplateManager.getConStaticTemplate(staticTemGoodsDto.getTemId());
                conStaticTemplate.setCreateTime(new Date());
                conStaticTemplate.setUpdateUser(staticTemGoodsDto.getUserName());
                GoodsBoxDto goodsBoxDto = new GoodsBoxDto();
                goodsBoxDto.setSellerNo(conStaticTemplate.getSellerNo());
                goodsBoxDto.setTemNo(conStaticTemplate.getTemNo());
                // 判断如果如果这个模板有绑定设备即柜子需要更新设备商品模板表
                // 当前模板下的所有设备的设备商品信息
                List<ConDeviceGoods> boxGoodsList = conDeviceGoodsManager.getBoxGoodsList(goodsBoxDto);

                staticTemGoodsDto.setTemNo(conStaticTemplate.getTemNo());
                staticTemGoodsDto.setBoxGoodsList(boxGoodsList);
                staticTemGoodsDto.setConStaticTemplate(conStaticTemplate);
                returnFlag = conStaticTemplateManager.updateStaticTemAndGoods(staticTemGoodsDto);
                if (returnFlag) {
                    result.setSuccess(true);
                    result.setResultCode(BaseConstants.SERVICE_RESULT_SUCCESS_CODE);
                    result.addDefaultModel(BaseConstants.SERVICE_RESULT_SUCCESS_CODE,
                            boxGoodsList);
                } else {
                    result.setSuccess(false);
                    result.setResultCode(BaseConstants.SERVICE_RESULT_FAIL_CODE);
                    result.addDefaultModel(BaseConstants.SERVICE_RESULT_FAIL_CODE,
                            BaseConstants.SERVICE_RESULT_FAIL_MESSAGE);
                }
            } else {
                result.setSuccess(false);
                result.setResultCode(BaseConstants.SERVICE_RESULT_FAIL_CODE);
                result.addDefaultModel(BaseConstants.SERVICE_RESULT_FAIL_CODE,
                        BaseConstants.SERVICE_RESULT_FAIL_MESSAGE);
            }
        } catch (Exception e) {
            logger.error("ConStaticTemplateServiceImpl!updateStaticTemAndGoods -> error!!", e);
            result.setSuccess(false);
        }
        return result;
    }

    @Override
    public Result validBoxTemGoods(StaticTemGoodsDto staticTemGoodsDto) {
        Result result = new Result();
        try {
            if (null != staticTemGoodsDto) {
                boolean returnFlag = true;
                //保存通用消息
                GoodsBoxDto goodsBoxDto = new GoodsBoxDto();
                goodsBoxDto.setSellerNo(staticTemGoodsDto.getSellerNo());
                goodsBoxDto.setTemNo(staticTemGoodsDto.getTemNo());
                // 该模板-原始设备-对应的商品
                List<ConDeviceGoods> boxGoodsList = conDeviceGoodsManager.getBoxGoodsList(goodsBoxDto);
                // 模板-商品编码
                ConTemGoodsQuery conTemGoodsQuery = new ConTemGoodsQuery();
                conTemGoodsQuery.setTemNo(staticTemGoodsDto.getTemNo());
                List<ConTemGoods> listTemoGoods = conTemGoodsManager.getConTemGoodsList(conTemGoodsQuery);
                List<String> listTemoGoodsNos = listTemoGoods.stream().map(p -> p.getGoodsNo()).distinct().collect(Collectors.toList());
                // 商家-商品信息
                ConSellerGoodsQuery conSellerGoodsQuery = new ConSellerGoodsQuery();
                conSellerGoodsQuery.setSellerNo(staticTemGoodsDto.getSellerNo());
                List<ConSellerGoods> listSellerGoods = conSellerGoodsManager.getConSellerGoodsList(conSellerGoodsQuery);
                // 过滤出该模板的商家商品全信息
                listSellerGoods = listSellerGoods.stream().filter(p -> listTemoGoodsNos.contains(p.getGoodsNo())).collect(Collectors.toList());
                // 用来存最终设备商品信息
                List<ConDeviceGoods> boxGoodsListFinal = new ArrayList<>();
                if (listSellerGoods.size() > 0) {
                    // 本次选择的商品信息列表-商品编码列表-新值
                    List<String> listGoodsNosNew = listSellerGoods.stream().map(p -> p.getGoodsNo()).collect(Collectors.toList());
                    // 设备商品信息列表-商品编码列表-原始值
                    List<String> listBoxGoodsNosOld = boxGoodsList.stream().map(p -> p.getGoodsNo()).distinct().collect(Collectors.toList());
                    // 生效的设备
                    List<String> listBoxNos = staticTemGoodsDto.getListBoxNos();
                    // 如果有设备商品则需要做一些操作
                    if (boxGoodsList.size() > 0) {
                        // 1、新模板有此商品,则不需要改变任何信息
                        boxGoodsListFinal = boxGoodsList.stream().filter(p -> listGoodsNosNew.contains(p.getGoodsNo())).
                                collect(Collectors.toList());
                        // 2、过滤商家当前模板商品-新增的模板商品
                        listSellerGoods = listSellerGoods.stream().filter(p -> !listBoxGoodsNosOld.contains(p.getGoodsNo())).
                                collect(Collectors.toList());
                    }

                    // 3、获取设备列表信息
                    ConBox conBox = new ConBox();
                    conBox.setTemNo(staticTemGoodsDto.getTemNo());
                    conBox.setConBoxNosList(staticTemGoodsDto.getListBoxNos());
                    List<ConBox> listBoxs = conDeviceManager.getConBoxListByTemNo(conBox);
                    for (ConBox conBoxItem : listBoxs
                    ) {
                        List<ConDeviceGoods> listComBoxGoods = listSellerGoods.stream()
                                .map(e -> new ConDeviceGoods(e.getSellerNo(), e.getSellerName(),
                                        conBoxItem.getBoxNo(), conBoxItem.getBoxName(),
                                        e.getGoodsNo(), e.getGoodsName(), staticTemGoodsDto.getTemNo(),
                                        staticTemGoodsDto.getTemName(), new Date(), new Date(),
                                        staticTemGoodsDto.getUserName(), staticTemGoodsDto.getUserName(),
                                        e.getSalePrice(), e.getSaleUnitRealPrice(),
                                        conBoxItem.getStoreNo(),conBoxItem.getStoreName()))
                                .collect(Collectors.toList());
                        // 追加list
                        boxGoodsListFinal.addAll(listComBoxGoods);
                    }
                }
                staticTemGoodsDto.setTemNo(staticTemGoodsDto.getTemNo());
                staticTemGoodsDto.setBoxGoodsList(boxGoodsListFinal);
                returnFlag = conStaticTemplateManager.validBoxTemGoods(staticTemGoodsDto);
                if (returnFlag) {
                    result.setSuccess(true);
                    result.setResultCode(BaseConstants.SERVICE_RESULT_SUCCESS_CODE);
                    result.addDefaultModel(BaseConstants.SERVICE_RESULT_SUCCESS_CODE,
                            boxGoodsListFinal);
                } else {
                    result.setSuccess(false);
                    result.setResultCode(BaseConstants.SERVICE_RESULT_FAIL_CODE);
                    result.addDefaultModel(BaseConstants.SERVICE_RESULT_FAIL_CODE,
                            BaseConstants.SERVICE_RESULT_FAIL_MESSAGE);
                }

            } else {
                result.setSuccess(false);
                result.setResultCode(BaseConstants.SERVICE_RESULT_FAIL_CODE);
                result.addDefaultModel(BaseConstants.SERVICE_RESULT_FAIL_CODE,
                        BaseConstants.SERVICE_RESULT_FAIL_MESSAGE);
            }
        } catch (Exception e) {
            logger.error("ConStaticTemplateServiceImpl!validBoxTemGoods -> error!!", e);
            result.setSuccess(false);
        }
        return result;
    }

    public void setConStaticTemplateManager(ConStaticTemplateManager conStaticTemplateManager) {
        this.conStaticTemplateManager = conStaticTemplateManager;
    }

    public void setBizSequenceManager(BizSequenceManager bizSequenceManager) {
        this.bizSequenceManager = bizSequenceManager;
    }

    public void setConDeviceGoodsManager(ConDeviceGoodsManager conDeviceGoodsManager) {
        this.conDeviceGoodsManager = conDeviceGoodsManager;
    }

    public void setConTemGoodsManager(ConTemGoodsManager conTemGoodsManager) {
        this.conTemGoodsManager = conTemGoodsManager;
    }

    public void setConDeviceManager(ConDeviceManager conDeviceManager) {
        this.conDeviceManager = conDeviceManager;
    }

    public void setConSellerGoodsManager(ConSellerGoodsManager conSellerGoodsManager) {
        this.conSellerGoodsManager = conSellerGoodsManager;
    }
}

 

posted on 2020-06-17 20:23  王洪洪  阅读(884)  评论(0编辑  收藏  举报