package com.gree.mall.manager.logic.workorder;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.SyncOrderDto;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.workorder.*;
import com.gree.mall.manager.commonmapper.workorder.IncreMapper;
import com.gree.mall.manager.constant.SysDictConstant;
import com.gree.mall.manager.enums.PayStatusEnum;
import com.gree.mall.manager.enums.admin.AdminWebsitTypeEnum;
import com.gree.mall.manager.enums.workorder.OrderBaseStatusEnum;
import com.gree.mall.manager.enums.workorder.OrderOperatorLogTypeEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.logic.common.SysDictCompanyLogic;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.plus.service.*;
import com.gree.mall.manager.utils.excel.ExcelUtils;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class ChangeOrderLogic {

    @Autowired
    PgOrderBaseService pgOrderBaseService;

    @Autowired
    UserService userService;

    @Autowired
    AdminWebsitService adminWebsitService;

    @Autowired
    PgOrderWorkerService pgOrderWorkerService;


    @Autowired
    PgOrderProductService pgOrderProductService;

    @Autowired
    PgOrderDetailService pgOrderDetailService;

    @Autowired
    WorkerOrderService workerOrderService;

    @Autowired
    WorkerOrderItemService workerOrderItemService;


    @Autowired
    OrderLogLogic orderLogLogic;

    @Autowired
    PgOrderProductDetailService pgOrderProductDetailService;

    @Autowired
    PgOrderOperatorLogService pgOrderOperatorLogService;

    @Autowired
    CommonLogic commonLogic;


    @Autowired
    IncreMapper increMapper;

    @Autowired
    RegionService regionService;


    @Autowired
    SysDictCompanyService sysDictCompanyService;

    @Autowired
    OrderSmallTypeService orderSmallTypeService;

    @Autowired
    GoodsCategoryService goodsCategoryService;

    @Autowired
    WebsitDispatchService websitDispatchService;

    @Autowired
    SysDictCompanyLogic sysDictCompanyLogic;

    @Autowired
    OrderBaseLogic orderBaseLogic;


    @Transactional(rollbackFor = Exception.class)
    public void changeAppointmentTime(String id, String appointmentTime, String appointmentEndTime,String remark) {
        pgOrderBaseService.lambdaUpdate()
                .eq(PgOrderBase::getId,id)
                .set(!StringUtils.isEmpty(remark),PgOrderBase::getChangeRemark,remark)
                .set(PgOrderBase::getAppointmentTime,appointmentTime)
                .set(PgOrderBase::getAppointmentEndTime,appointmentEndTime)
                .set(PgOrderBase::getLastOperatorAppointmentTime,new Date())
                .update();

//        pgOrderBaseService.lambdaUpdate()
//                .eq(PgOrderBase::getId,id)
//                .eq(PgOrderBase::getOrderStatus,OrderBaseStatusEnum.DYY.getKey())
//                .set(PgOrderBase::getOrderStatus,OrderBaseStatusEnum.DSHPG).update();

        this.addLog(id);
    }

    private void addLog(String id) {
        PgOrderBase pgOrderBase = pgOrderBaseService.getById(id);

        PgOrderOperatorLog pgOrderOperatorLog = new PgOrderOperatorLog();
        pgOrderOperatorLog.setOrderBaseId(id);
        pgOrderOperatorLog.setType(OrderOperatorLogTypeEnum.GAIYUE.getRemark());
        pgOrderOperatorLog.setTypeText(OrderOperatorLogTypeEnum.GAIYUE.getRemark());
        pgOrderOperatorLog.setAppointmentTime(pgOrderBase.getAppointmentTime());
        pgOrderOperatorLog.setAppointmentEndTime(pgOrderBase.getAppointmentEndTime());
        pgOrderOperatorLog.setContent(pgOrderBase.getChangeRemark());
        orderLogLogic.addLog(pgOrderOperatorLog);
    }

    @Transactional(rollbackFor = Exception.class)
    public void changeWorker(List<String> ids, List<String> workerIds, String websitId) {

        ids = ids.stream().distinct().collect(Collectors.toList());
        workerIds = workerIds.stream().distinct().collect(Collectors.toList());
        //循环师傅获得网点派工数据
        AdminWebsit adminWebsit = adminWebsitService.getById(websitId);

        List<PgOrderWorker> pgOrderWorkers = new ArrayList<>();



        boolean isDa = true;

        User userOne = userService.getById(workerIds.get(0));
        for (String workerId : workerIds) {
            User user = userService.getById(workerId);
            for (String id : ids) {

                PgOrderWorker pgOrderWorker = new PgOrderWorker();
                pgOrderWorker.setOrderBaseId(id);
                pgOrderWorker.setWebsitId(adminWebsit.getWebsitId());
                pgOrderWorker.setWebsitName(adminWebsit.getName());
                pgOrderWorker.setWorkerName(user.getNickName());
                pgOrderWorker.setWorkerId(workerId);
                pgOrderWorker.setWorkerIdcard(user.getIdCard());
                pgOrderWorker.setWorkerMobile(user.getMobile());
                if (userOne.getIdCard().equals(user.getIdCard()))
                    pgOrderWorker.setIsMaster(isDa);
                else
                    pgOrderWorker.setIsMaster(false);
                pgOrderWorkers.add(pgOrderWorker);
            }

        }


        pgOrderBaseService.lambdaUpdate()
                .in(PgOrderBase::getId,ids)
                .set(PgOrderBase::getWorkerName,userOne.getNickName())
                .set(PgOrderBase::getWebsitId,websitId)
                .set(PgOrderBase::getWebsitName,adminWebsit.getName())
                .set(PgOrderBase::getWebsitPhone,adminWebsit.getWebsitPhone())
                .set(PgOrderBase::getOrderStatus,OrderBaseStatusEnum.DJD.getKey())
                .set(PgOrderBase::getOrderStatusText,OrderBaseStatusEnum.DJD.getRemark())
                .set(PgOrderBase::getWorkerIdcard,userOne.getIdCard())
                .set(PgOrderBase::getWorkerId,userOne.getUserId())
                .set(PgOrderBase::getWorkerMobile,userOne.getMobile())
                .set(PgOrderBase::getDispatchTime,new Date())
                .set(PgOrderBase::getIsQd,false)
                .update();


        pgOrderWorkerService.lambdaUpdate().in(PgOrderWorker::getOrderBaseId,ids).remove();

        pgOrderWorkerService.saveBatch(pgOrderWorkers);

        for (String id : ids) {
            this.addWorkerLog(id);
        }
    }

    private void addWorkerLog(String id) {
        PgOrderBase pgOrderBase = pgOrderBaseService.getById(id);

        PgOrderOperatorLog pgOrderOperatorLog = new PgOrderOperatorLog();
        pgOrderOperatorLog.setOrderBaseId(id);
        pgOrderOperatorLog.setType(OrderOperatorLogTypeEnum.GP.getKey());
        pgOrderOperatorLog.setTypeText(OrderOperatorLogTypeEnum.GP.getRemark());
        pgOrderOperatorLog.setContent("派工主要工程师："+pgOrderBase.getWorkerName());
        orderLogLogic.addLog(pgOrderOperatorLog);
    }


    public List<PgOrderProductDetail> getOrderProduct(String id, List<String> status) {

        return pgOrderProductDetailService.lambdaQuery().eq(PgOrderProductDetail::getOrderBaseId,id)
                .in(!CollectionUtils.isEmpty(status),PgOrderProductDetail::getStatus,status).list();
    }

    public PgOrderProductDetailVO productDetail(String id) {
        PgOrderProductDetail pgOrderProduct = pgOrderProductDetailService.getById(id);

        PgOrderProductDetailVO pgOrderProductDetail = BeanUtil.toBean(pgOrderProduct, PgOrderProductDetailVO.class);

        List<PgOrderDetail> list = pgOrderDetailService.lambdaQuery().eq(PgOrderDetail::getProductDetailId, id).list();

        pgOrderProductDetail.setPgOrderProductDetails(list);
        PgOrderBase pgOrderBase = pgOrderBaseService.getById(pgOrderProduct.getOrderBaseId());

        pgOrderProductDetail.setPgOrderBase(pgOrderBase);

        List<PgOrderWorker> list1 = pgOrderWorkerService.lambdaQuery().eq(PgOrderWorker::getOrderBaseId, pgOrderProductDetail.getOrderBaseId()).list();
        pgOrderProductDetail.setPgOrderWorkers(list1);

        return pgOrderProductDetail;

    }


    public List<WorkerOrderDetail> getOrderList(String workerOrderId) {

        List<WorkerOrder> list = workerOrderService.lambdaQuery().eq(WorkerOrder::getWorkerOrderId, workerOrderId)
                .eq(WorkerOrder::getPayStatus, PayStatusEnum.PAID.getKey()).list();
        if (CollectionUtils.isEmpty(list))
            return new ArrayList<>();

        List<WorkerOrderDetail> workerOrderDetails1 = BeanUtil.copyToList(list, WorkerOrderDetail.class);
        for (WorkerOrderDetail workerOrder : workerOrderDetails1) {

            workerOrder.setWorkerOrderItems(workerOrderItemService.lambdaQuery()
                    .eq(WorkerOrderItem::getOrderId,workerOrder.getOrderId()).list());

            PgOrderBase pgOrderBase = pgOrderBaseService.getById(workerOrderId);
            workerOrder.setPgOrderBase(pgOrderBase);


        }




        return workerOrderDetails1;

    }

    public IPage<DispatchVO> list(ZfireParamBean zfireParam) {

        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        //1.组装查询条件
        zfireParam = FieldUtils.supplyParam(zfireParam, DispatchVO.class);

        IPage<DispatchVO> dispatchVOIPage = increMapper.listDispatch(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam,adminCompanyIds);

        return dispatchVOIPage;


    }

    @Transactional(rollbackFor = Exception.class)
    public void saveOrder(WebsitDispatch websitDispatch) {
        AdminUserCom adminUser = commonLogic.getAdminUser();

        websitDispatch.setCompanyWechatId(adminUser.getCompanyWechatId());
        websitDispatch.setCompanyWechatName(adminUser.getCompanyName());
        websitDispatch.insert();
    }

    public IPage<WebsitDispatchVO> listDispatch(ZfireParamBean zfireParamBean) {

        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        //1.组装查询条件
        zfireParamBean = FieldUtils.supplyParam(zfireParamBean, WebsitDispatchVO.class);

        IPage<WebsitDispatchVO> websitDispatchVOIPage = increMapper.listDispatchWebsit(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean,adminCompanyIds);

        return websitDispatchVOIPage;
    }

    public List<DispatchCherVO> pList(String pId, String cityCode, String areaCode, String streetCode, List<String> dictCode, List<String> orderSmallId, List<String> categoryId, List<String> orderSourceId) {
        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        return increMapper.pList(pId,  cityCode,  areaCode,  streetCode,  dictCode,  orderSmallId,  categoryId, orderSourceId,adminCompanyIds);
    }

    public List<DispatchCherVO> cList(String pId, String cityCode, String areaCode, String streetCode,List<String> dictCode, List<String> orderSmallId, List<String> categoryId, List<String> orderSourceId) {
        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        return increMapper.cList(pId,  cityCode,  areaCode,  streetCode,  dictCode,  orderSmallId,  categoryId, orderSourceId,adminCompanyIds);
    }

    public List<DispatchCherVO> aList(String pId, String cityCode, String areaCode, String streetCode, List<String> dictCode, List<String> orderSmallId, List<String> categoryId, List<String> orderSourceId) {
        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        return increMapper.aList(pId,  cityCode,  areaCode,  streetCode,  dictCode,  orderSmallId,  categoryId, orderSourceId,adminCompanyIds);
    }

    public List<DispatchCherVO> sList(String pId, String cityCode, String areaCode, String streetCode, List<String> dictCode, List<String> orderSmallId, List<String> categoryId, List<String> orderSourceId) {
        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        return increMapper.sList(pId,  cityCode,  areaCode,  streetCode,  dictCode,  orderSmallId,  categoryId, orderSourceId,adminCompanyIds);
    }

    public List<DispatchCherVO> channel(String pId, String cityCode, String areaCode, String streetCode, List<String> dictCode, List<String> orderSmallId, List<String> categoryId, List<String> orderSourceId) {
        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        return increMapper.channel(pId,  cityCode,  areaCode,  streetCode,  dictCode,  orderSmallId,  categoryId, orderSourceId,adminCompanyIds);
    }

    public List<DispatchCherVO> smallList(String pId, String cityCode, String areaCode, String streetCode, List<String> dictCode, List<String> orderSmallId, List<String> categoryId, List<String> orderSourceId) {
        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        return increMapper.smallList(pId,  cityCode,  areaCode,  streetCode,  dictCode,  orderSmallId,  categoryId, orderSourceId,adminCompanyIds);
    }

    public List<DispatchCherVO> categoryList(String pId, String cityCode, String areaCode, String streetCode, List<String> dictCode, List<String> orderSmallId, List<String> categoryId, List<String> orderSourceId) {
        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        return increMapper.categoryList(pId,  cityCode,  areaCode,  streetCode,  dictCode,  orderSmallId,  categoryId, orderSourceId,adminCompanyIds);
    }

    public List<DispatchCherVO> sourceList(String pId, String cityCode, String areaCode, String streetCode, List<String> dictCode, List<String> orderSmallId, List<String> categoryId, List<String> orderSourceId) {
        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        return increMapper.sourceList(pId,  cityCode,  areaCode,  streetCode,  dictCode,  orderSmallId,  categoryId, orderSourceId,adminCompanyIds);
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveDispatch(WebsitDispatchAdd websitDispatchAdd) {
        AdminWebsit adminWebsit = adminWebsitService.getById(websitDispatchAdd.getWebsitId());

        Region region = regionService.getById(websitDispatchAdd.getStreetCode());

        List<WebsitDispatch> websitDispatches = new ArrayList<>();

        for (String orderSmallId : websitDispatchAdd.getOrderSmallIds()) {

            OrderSmallType orderSmallType = orderSmallTypeService.getById(orderSmallId);

            for (String categoryId : websitDispatchAdd.getCategoryIds()) {

                GoodsCategory goodsCategory = goodsCategoryService.getById(categoryId);

                for (String orderChannel : websitDispatchAdd.getOrderChannelIds()) {

                    SysDictCompany orderChanne = sysDictCompanyService.lambdaQuery().eq(SysDictCompany::getDictCode, orderChannel).last("limit 1").one();

                    for (String orderSourceId : websitDispatchAdd.getOrderSourceIds()) {


                        SysDictCompany orderSource = sysDictCompanyService.lambdaQuery().eq(SysDictCompany::getDictCode, orderSourceId).last("limit 1").one();

                        WebsitDispatch websitDispatch = new WebsitDispatch();
                        websitDispatch.setWebsitId(adminWebsit.getWebsitId());
                        websitDispatch.setWebsitName(adminWebsit.getName());
                        websitDispatch.setProvinceCode(region.getProvinceCode());
                        websitDispatch.setProvinceName(region.getProvinceName());
                        websitDispatch.setCityCode(region.getCityCode());
                        websitDispatch.setCityName(region.getCityName());
                        websitDispatch.setAreaCode(region.getAreaCode());
                        websitDispatch.setAreaName(region.getAreaName());
                        websitDispatch.setStreetCode(region.getId());
                        websitDispatch.setStreetName(region.getName());
                        websitDispatch.setCompanyWechatId(adminWebsit.getCompanyWechatId());
                        websitDispatch.setCompanyWechatName(adminWebsit.getCompanyWechatName());


                        websitDispatch.setOrderSmallTypeText(orderSmallType.getOrderSmallTypeText());
                        websitDispatch.setOrderSmallId(orderSmallType.getId());

                        websitDispatch.setCategoryId(goodsCategory.getCategoryId());
                        websitDispatch.setCategoryName(goodsCategory.getName());

                        websitDispatch.setDictCode(orderChanne.getDictCode());
                        websitDispatch.setDictValue(orderChanne.getDictValue());

                        websitDispatch.setOrderSource(orderSource.getDictValue());
                        websitDispatch.setOrderSourceId(orderSource.getDictCode());

                        if (websitDispatchService.lambdaQuery().eq(WebsitDispatch::getStreetCode,region.getId())
                                .eq(WebsitDispatch::getOrderSmallId,websitDispatch.getOrderSmallId())
                                .eq(WebsitDispatch::getCategoryId,websitDispatch.getCategoryId())
                                .eq(WebsitDispatch::getDictCode,websitDispatch.getDictCode())
                                .eq(WebsitDispatch::getOrderSourceId,websitDispatch.getOrderSourceId()).count() > 0) {
                            throw new RemoteServiceException("存在街道："+websitDispatch.getStreetName()+"，产品大类："+websitDispatch.getCategoryName()+
                                    "，工单渠道："+websitDispatch.getDictValue()+"，工单来源："+websitDispatch.getOrderSource()+"，工单类型："+websitDispatch.getOrderSmallTypeText());
                        }

                        websitDispatches.add(websitDispatch);

                    }
                }
            }
        }

        websitDispatchService.saveBatch(websitDispatches);
    }

    public void delDispatch(List<String> id) {
        websitDispatchService.removeByIds(id);
    }



    @Transactional(rollbackFor = Exception.class)
    public void importData(MultipartFile file) throws IOException {

        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<Object> rows = ExcelUtils.importExcel(file);
        if(org.apache.commons.collections4.CollectionUtils.isEmpty(rows)){
            throw new RemoteServiceException("导入内容为空");
        }


        //工单类型map
        Map<String, OrderSmallType> orderSmallTypeMap = sysDictCompanyLogic.orderSmallTypeMap(adminUser.getCompanyWechatId());
        //产品大类，小类map
        List<GoodsCategory> goodsCategoryList = goodsCategoryService.lambdaQuery()
                .eq(GoodsCategory::getCompanyWechatId, adminUser.getCompanyWechatId())
                .eq(GoodsCategory::getType, 2).list();
        Map<String, GoodsCategory> categoryMap1 = goodsCategoryList.stream().filter(v -> v.getLevel() == 1)
                .collect(Collectors.toMap(GoodsCategory::getName, v -> v));

        //工单渠道
         Map<String, SysDictCompany> orderChannelMap = sysDictCompanyLogic.dictByTypeOfValue(SysDictConstant.ORDER_CHANNEL, adminUser.getCompanyWechatId());

        //工单渠道
        Map<String, SysDictCompany> orderSourceMap = sysDictCompanyLogic.dictByTypeOfValue(SysDictConstant.ORDER_SOURCE, adminUser.getCompanyWechatId());
        //网点名称
        Map<String, AdminWebsit> adminWebsitMap = adminWebsitService.lambdaQuery()
                .eq(AdminWebsit::getCompanyWechatId, adminUser.getCompanyWechatId())
                .in(adminUser.getType() == 0, AdminWebsit::getWebsitId, adminUser.getAdminWebsitIds())
                .eq(AdminWebsit::getType, AdminWebsitTypeEnum.C.getKey())
                .list().stream().collect(Collectors.toMap(AdminWebsit::getWebsitId, Function.identity()));

        List<WebsitDispatch> websitDispatches = new ArrayList<>();
        for(Object o : rows) {
            List<Object> row = (List<Object>) o;

            String websitName = (String)row.get(0);
            String websitId = (String)row.get(1);
            String pName = (String)row.get(2);
            String cName = (String)row.get(3);
            String aName = (String)row.get(4);
            String sName = (String)row.get(5);
            String channelName = (String)row.get(6);
            String smallName = (String)row.get(7);
            String categoryName = (String)row.get(8);
            String sourceName = (String)row.get(9);

            AdminWebsit adminWebsit = adminWebsitMap.get(websitId);

            Region region = orderBaseLogic.getRegion(pName, cName, aName, sName);

            SysDictCompany orderChanne = orderChannelMap.get(channelName);

            GoodsCategory goodsCategory = categoryMap1.get(categoryName);

            OrderSmallType orderSmallType = orderSmallTypeMap.get(smallName);

            SysDictCompany orderSource = orderSourceMap.get(sourceName);

            WebsitDispatch websitDispatch = new WebsitDispatch();
            websitDispatch.setWebsitId(adminWebsit.getWebsitId());
            websitDispatch.setWebsitName(adminWebsit.getName());
            websitDispatch.setProvinceCode(region.getProvinceCode());
            websitDispatch.setProvinceName(region.getProvinceName());
            websitDispatch.setCityCode(region.getCityCode());
            websitDispatch.setCityName(region.getCityName());
            websitDispatch.setAreaCode(region.getAreaCode());
            websitDispatch.setAreaName(region.getAreaName());
            websitDispatch.setStreetCode(region.getId());
            websitDispatch.setStreetName(region.getName());
            websitDispatch.setCompanyWechatId(adminUser.getCompanyWechatId());
            websitDispatch.setCompanyWechatName(adminUser.getCompanyName());


            websitDispatch.setOrderSmallTypeText(orderSmallType.getOrderSmallTypeText());
            websitDispatch.setOrderSmallId(orderSmallType.getId());

            websitDispatch.setCategoryId(goodsCategory.getCategoryId());
            websitDispatch.setCategoryName(goodsCategory.getName());

            websitDispatch.setDictCode(orderChanne.getDictCode());
            websitDispatch.setDictValue(orderChanne.getDictValue());

            websitDispatch.setOrderSource(orderSource.getDictValue());
            websitDispatch.setOrderSourceId(orderSource.getDictCode());

            if (websitDispatchService.lambdaQuery().eq(WebsitDispatch::getStreetCode,region.getId())
                    .eq(WebsitDispatch::getOrderSmallId,websitDispatch.getOrderSmallId())
                    .eq(WebsitDispatch::getCategoryId,websitDispatch.getCategoryId())
                    .eq(WebsitDispatch::getDictCode,websitDispatch.getDictCode())
                    .eq(WebsitDispatch::getOrderSourceId,websitDispatch.getOrderSourceId()).count() > 0) {
                throw new RemoteServiceException("存在街道："+websitDispatch.getStreetName()+"，产品大类："+websitDispatch.getCategoryName()+
                        "，工单渠道："+websitDispatch.getDictValue()+"，工单来源："+websitDispatch.getOrderSource()+"，工单类型："+websitDispatch.getOrderSmallTypeText());
            }

            websitDispatches.add(websitDispatch);

        }

        websitDispatchService.saveBatch(websitDispatches);

    }

    @Transactional(rollbackFor = Exception.class)
    public void updateDispatch(WebsitDispatch websitDispatch) {

        if (websitDispatchService.lambdaQuery().eq(WebsitDispatch::getStreetCode,websitDispatch.getStreetCode())
                .eq(WebsitDispatch::getOrderSmallId,websitDispatch.getOrderSmallId())
                .eq(WebsitDispatch::getCategoryId,websitDispatch.getCategoryId())
                .eq(WebsitDispatch::getDictCode,websitDispatch.getDictCode())
                .ne(WebsitDispatch::getId,websitDispatch.getId())
                .eq(WebsitDispatch::getOrderSourceId,websitDispatch.getOrderSourceId()).count() > 0) {
            throw new RemoteServiceException("存在街道："+websitDispatch.getStreetName()+"，产品大类："+websitDispatch.getCategoryName()+
                    "，工单渠道："+websitDispatch.getDictValue()+"，工单来源："+websitDispatch.getOrderSource()+"，工单类型："+websitDispatch.getOrderSmallTypeText());
        }

        websitDispatch.updateById();

    }

    public WebsitDispatch detailDispatch(String id) {

        return websitDispatchService.getById(id);
    }
}
