Newer
Older
huludao / src / main / java / com / newfiber / api / pc / service / impl / AllocServiceImpl.java
package com.newfiber.api.pc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.newfiber.api.core.commons.*;
import com.newfiber.api.pc.dao.AllocMapper;
import com.newfiber.api.pc.dao.ReturnGoodsMapper;
import com.newfiber.api.pc.dto.DataOperationDTO;
import com.newfiber.api.pc.dto.SiteDataOperationDTO;
import com.newfiber.api.pc.dto.SupAllotDTO;
import com.newfiber.api.pc.model.meet.AcceptAllot;
import com.newfiber.api.pc.model.meet.AcceptSup;
import com.newfiber.api.pc.model.meet.HouseSiteSupplies;
import com.newfiber.api.pc.model.vo.HoutSiteReturnVo;
import com.newfiber.api.pc.model.vo.SuppliesReturnVo;
import com.newfiber.api.pc.service.AcceptSupService;
import com.newfiber.api.pc.service.AllocService;
import com.newfiber.api.pc.service.HouseSiteSuppliesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

/**
 * 调度管理业务层实现类
 * @Author:zhz
 * @CreateDate:2020/12/1 14:47
 * @Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AllocServiceImpl extends ServiceImpl<AllocMapper,AcceptAllot> implements AllocService {

    @Autowired
    private AcceptSupService acceptSupService;

    @Autowired
    private HouseSiteSuppliesService houseSiteSuppliesService;

    @Autowired
    private IsHightAuthUtils isHightAuthUtils;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private AllocMapper allocMapper;
    @Autowired
    private ReturnGoodsMapper returnGoodsMapper;

    @Override
    public void alloc(SupAllotDTO supAllotDTO, String userNo) {
        //扣减转出站点中的物资数量
        AcceptAllot allot = new AcceptAllot();
        //指定接收人
        allot.setAssignUserNo(supAllotDTO.getAssignUserNo());
        //调度人
        allot.setAllotUserNo(userNo);
        allot.setCreateTime(new Date());
        //调度原由
        allot.setAllotName(supAllotDTO.getAllotName());
        //设置为未接收状态
        allot.setState(1);
        this.insert(allot);
        //利用线程池去调用扣减物资操作
        ExecutorService service = Executors.newFixedThreadPool(2);
        List<AcceptSup> supList = new ArrayList<>();
        for(SiteDataOperationDTO dto : supAllotDTO.getDtoList()){
            service.submit(() -> {
                //扣减物资
                houseSiteSuppliesService.deductionSupplies(null,dto.getHId(),dto.getDataOperationDTO());
            });
            for(DataOperationDTO dataOperationDTO : dto.getDataOperationDTO()){
                AcceptSup acceptSup = new AcceptSup();
                //添加接收站点id
                acceptSup.setAcceptHouseId(supAllotDTO.getAcceptHouseId());
                //设置关联数据
                acceptSup.setOtId(allot.getAllotId());
                //设置转出站点id
                acceptSup.setRollOutHouseSiteId(dto.getHId());
                //设置转出物资编号
                acceptSup.setRollOutSupId(dataOperationDTO.getSupId());
                //设置转出物资数量
                acceptSup.setRollOutSupCount(dataOperationDTO.getCount());
                //添加物资名称
                acceptSup.setRollOutSupName(dataOperationDTO.getSupName());
                //添加到集合
                supList.add(acceptSup);
            }
        }
        //插入暂存物资表
        acceptSupService.insertBatch(supList);

        //关闭线程池
        service.shutdown();

    }

    @Override
    public PageResultObject<AcceptAllot> serchAcceptList(PageRequestObject<Integer> page, HttpServletRequest request) {
        String userNo = request.getHeader("userNo");
        String token = request.getHeader("token");
        Page<AcceptAllot> objectPage = new Page<>(page.getCurrent(), page.getSize());
        Integer state = page.getObject();
        if(StringUtils.isEmpty(state)){
            throw new CustomException(ResultCode.PARAM_NULL);
        }
        EntityWrapper<AcceptAllot> wrapper = new EntityWrapper<>();
        wrapper.eq("state",state);
        //不是最高权限,就查自己的,是的话就查全部
        if(!isHightAuthUtils.isAuth(token)){
            wrapper.eq("assign_user_no",userNo);
        }
        wrapper.orderDesc(Arrays.asList(new String[] {"create_time"}));

        Page<AcceptAllot> allotPage = this.selectPage(objectPage, wrapper);
        int count = this.selectCount(wrapper);
        return new PageResultObject<>(page.getCurrent(),page.getSize(),(long)count,allotPage.getRecords());
    }

    @Override
    public PageResultObject<AcceptAllot> serchAllot(PageRequestObject<Integer> page, HttpServletRequest request) {
        String userNo = request.getHeader("userNo");
        String token = request.getHeader("token");
        Page<AcceptAllot> objectPage = new Page<>(page.getCurrent(), page.getSize());
        Integer state = page.getObject();
        if(StringUtils.isEmpty(state)){
            throw new CustomException(ResultCode.PARAM_NULL);
        }
        EntityWrapper<AcceptAllot> wrapper = new EntityWrapper<>();
        wrapper.eq("state",state);
        //不是最高权限,就查自己的,是的话就查全部
        if(!isHightAuthUtils.isAuth(token)){
            wrapper.eq("allot_user_no",userNo);
        }
        Page<AcceptAllot> allotPage = this.selectPage(objectPage, wrapper);
        int count = this.selectCount(wrapper);
        return new PageResultObject<>(page.getCurrent(),page.getSize(),(long)count,allotPage.getRecords());
    }


    @Override
    public void acceptSup(Integer allotId, HttpServletRequest request) {
        if(StringUtils.isEmpty(allotId)){
            throw new CustomException(ResultCode.PARAM_NULL);
        }
        //获取到所有调度过来的物资
        AcceptAllot allot = this.selectById(allotId);
        EntityWrapper<AcceptSup> wrapper = new EntityWrapper<>();
        wrapper.eq("ot_Id",allotId);
        List<AcceptSup> acceptSups = acceptSupService.selectList(wrapper);
        Integer hId = acceptSups.get(0).getAcceptHouseId();
        //查询接收站点内已有的物资,然后根据传入的物资判断是否有重复的,有重复的就增加数量,没有的新插入
        //因为接收站点被限制了只会有一个,所以可以直接取值
        EntityWrapper<HouseSiteSupplies> supWrapper = new EntityWrapper<>();
        supWrapper.eq("h_id",hId);
        //获取到该站点所有的物资列表
        List<HouseSiteSupplies> houseSiteSupplies = houseSiteSuppliesService.selectList(supWrapper);
        //循环比对
        for(HouseSiteSupplies siteSupplies : houseSiteSupplies){
            for(AcceptSup acceptSup : acceptSups){
                if(siteSupplies.getSupName().equals(acceptSup.getRollOutSupName())){
                    //执行增加数量操作
                    siteSupplies.setSupCount(siteSupplies.getSupCount() + acceptSup.getRollOutSupCount());
                    //删除当前
                    acceptSups.remove(acceptSup);
                    break;
                }
            }
        }
        //acceptSups集合中还留下的就是需要新增的物资数据,
        if(acceptSups.size() >  0){
            List<Integer> supIds = acceptSups.stream().map(AcceptSup::getRollOutSupId).collect(Collectors.toList());
            //去查询相关数据
            List<HouseSiteSupplies> siteSupplies = houseSiteSuppliesService.selectBatchIds(supIds);
            //构建对象
            if(CollectionUtil.isNotEmpty(siteSupplies)){
                for(HouseSiteSupplies sup : siteSupplies){
                    for(AcceptSup accSup : acceptSups){
                        if(sup.getSupId().equals(accSup.getRollOutSupId())){
                            sup.setSupId(null);
                            sup.setHId(hId);
                            sup.setSupCount(accSup.getRollOutSupCount());
                        }
                    }
                }
                //批量新增数据
                houseSiteSuppliesService.insertBatch(siteSupplies);
                houseSiteSupplies.addAll(siteSupplies);

                allot.setState(2);
                this.updateById(allot);
                houseSiteSuppliesService.updateBatchById(houseSiteSupplies);
                //将数据更新到Redis中
                String key = "supplies:" + hId;
                stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(houseSiteSupplies));
            }
        }
    }


    @Override
    public AcceptAllot viewInfo(Integer allotId) {
        if(StringUtils.isEmpty(allotId)){
            throw new CustomException(ResultCode.PARAM_NULL);
        }
        AcceptAllot acceptAllot = allocMapper.selectAllotByAllotId(allotId);
        List<Integer> hIds = acceptAllot.getSups().stream().map(AcceptSup::getRollOutHouseSiteId).collect(Collectors.toList());
        List<Integer> supId = acceptAllot.getSups().stream().map(AcceptSup::getRollOutSupId).collect(Collectors.toList());
        //根据多个存放点id
        List<HoutSiteReturnVo> houtSiteReturnVo = returnGoodsMapper.selectReturnGoods(hIds, supId);
        acceptAllot.getSups().stream().forEach(s -> {
            for(HoutSiteReturnVo vo : houtSiteReturnVo){
                for(SuppliesReturnVo returnVo : vo.getSupVo()){
                    if(s.getRollOutSupId().equals(returnVo.getSupId())){
                        returnVo.setSupCount(s.getRollOutSupCount());
                        break;
                    }
                }
            }
        });
        List<HoutSiteReturnVo> houseSites = new ArrayList<>();
        houseSites.addAll(houtSiteReturnVo);
        acceptAllot.setHouseSites(houseSites);
        return acceptAllot;
    }
}