/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.youchain.businessdata.rest;

import com.youchain.DuplicateSubmission.DuplicateSubmission;
import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.basicdata.domain.Area;
import com.youchain.basicdata.domain.BillType;
import com.youchain.basicdata.domain.Item;
import com.youchain.basicdata.domain.Point;
import com.youchain.basicdata.repository.BillTypeRepository;
import com.youchain.basicdata.service.PointService;
import com.youchain.businessdata.domain.*;
import com.youchain.businessdata.inputJson.IPTask;
import com.youchain.businessdata.inputJson.IScanPut;
import com.youchain.businessdata.inputJson.PutawayRequest;
import com.youchain.businessdata.inputJson.buttenJson.PickTicketButton;
import com.youchain.businessdata.repository.*;
import com.youchain.businessdata.returnJson.PutawayRet;
import com.youchain.businessdata.returnJson.RPPickList;
import com.youchain.businessdata.returnJson.RPTaskList;
import com.youchain.businessdata.returnJson.XdInventory;
import com.youchain.businessdata.service.*;
import com.youchain.businessdata.service.dto.PickTicketDto;
import com.youchain.businessdata.service.dto.PickTicketQueryCriteria;
import com.youchain.exception.BadRequestException;
import com.youchain.modules.system.domain.Dept;
import com.youchain.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.security.Security;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

import static org.springframework.http.HttpStatus.BAD_REQUEST;

/**
* @website https://eladmin.vip
* @author JiangKun
* @date 2024-01-11
**/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "pickTicket管理")
@RequestMapping("/api/pickTicket")
public class PickTicketController {
    private final PickTicketService pickTicketService;
    private final PickTicketRepository pickTicketRepository;
    private final PickDetailRepository pickDetailRepository;
    private final TaskRepository taskRepository;
    private final TaskLogRepository taskLogRepository;
    private final XppRecordService xppRecordService;
    private final XppRecordRepository xppRecordRepository;
    private final InventoryRepository inventoryRepository;
    private final BillTypeRepository billTypeRepository;
    private final TaskService taskService;
    private final TaskLogService taskLogService;
    private final InventoryService inventoryService;
    private final InventoryLogService inventoryLogService;
    private final PointService pointService;
    private final ItemKeyService itemKeyService;
    private final CodeUtils codeUtils;
    private final PickDetailService pickDetailService;


    @GetMapping("/getIdByPickTicket")
    @Log("查询pickTicket")
    @ApiOperation("查询pickTicket")
    @AnonymousAccess
    public ResponseEntity<Object> getIdByPickTicket(@RequestParam("id") Long id) {
        return new ResponseEntity<>(pickTicketRepository.findById(id), HttpStatus.OK);
    }


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @AnonymousAccess
    public void exportPickTicket(HttpServletResponse response, PickTicketQueryCriteria criteria) throws Exception {
        pickTicketService.download(pickTicketService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询pickTicket")
    @ApiOperation("查询pickTicket")
    @AnonymousAccess
    public ResponseEntity<Object> queryPickTicket(PickTicketQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(pickTicketService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PutMapping
    @Log("修改pickTicket")
    @ApiOperation("修改pickTicket")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> updatePickTicket(@Validated @RequestBody PickTicket resources ){
        pickTicketService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除pickTicket")
    @ApiOperation("删除pickTicket")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> deletePickTicket(@RequestBody Long[] ids) {
        //删除手工备货操作需要进行数量回退
        pickTicketService.deletePickTicket(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @PostMapping("/queryPickListApp")
    @Log("备货列表查询")
    @ApiOperation("备货列表查询")
    @AnonymousAccess
    public ResponseEntity<Object> queryPickListApp(@RequestBody IScanPut t){
        List<RPPickList> ts= pickTicketService.queryPickList(t);
        return new ResponseEntity<>(ts,HttpStatus.OK);
    }

    @PostMapping("/queryPickTaskListApp")
    @Log("备货列表明细查询")
    @ApiOperation("备货列表明细查询")
    @AnonymousAccess
    public ResponseEntity<Object> queryPickTaskListApp(@RequestBody IPTask t){
        List<RPTaskList> ts= taskService.queryTaskApp(t);
        return new ResponseEntity<>(ts,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增pickTicket")
    @ApiOperation("新增pickTicket")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> createPickTicket(@Validated @RequestBody PickTicket resources){
        if(resources.getBillType().getName().equals(BaseStatus.BT_QTCK)) {
            resources.setCode(codeUtils.getCode_yyMMdd(resources.getBillType().getPrefix(), 3));
        }
        return new ResponseEntity<>(pickTicketService.create(resources),HttpStatus.CREATED);
    }


    @PostMapping("/pickingOperations-del")
    @Log("拣货操作")
    @ApiOperation("拣货操作")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> pickingOperations(@RequestBody PickTicketButton resources){
        pickTicketService.pickingOperation(resources);
        return new ResponseEntity<>("操作成功",HttpStatus.OK);
    }

    @PostMapping("/xdckPick")
    @Log("箱单出库创建")
    @ApiOperation("箱单出库创建")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> xdckPick(@RequestBody String stockCode){
        //检查该托盘是否还有库存
        List<Inventory> invs=inventoryRepository.queryInvStockCode(stockCode);
        if(invs.size()<=0){
            throw new BadRequestException("没有对应的库存");
        }
        BillType bt=billTypeRepository.findByName(BaseStatus.XDBH);
        if(bt==null){
            throw new BadRequestException("维护箱单出库"+BaseStatus.XDBH+"类型");
        }
        Area outArea=invs.get(0).getArea();
        PickTicket pickTicket=pickTicketService.storePick(bt,null,outArea,"大",stockCode);
        double orderQty=0;
        //生成出库明细
        for (Inventory inv:invs){
            PickDetail detail=pickDetailService.createPickDetail(pickTicket,inv.getItemKey().getItem(), stockCode,inv.getQuantity(),null,BaseStatus.ZCCK);
            orderQty+=detail.getOrderQty();
            //分配
            pickDetailService.allocate(detail.getId(), detail.getOrderQty(),stockCode,BaseStatus.ZCCK);
        }
        pickTicket.setOrderQuantity(orderQty);
        pickTicketService.update(pickTicket);
        return new ResponseEntity<>("操作成功",HttpStatus.OK);
    }

    @PostMapping("/xdckAllocate")
    @Log("箱单出库分配")
    @ApiOperation("箱单出库分配")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> xdckAllocate(@RequestBody Long[] ids){
        for (Long id :ids) {
            List<PickDetail> pickTickets =pickDetailRepository.queryByPickTicketId(id);
            for (PickDetail pickDetail : pickTickets) {
                double unQty=pickDetail.getOrderQty()-pickDetail.getAllocatedQty();
                if(unQty>0) {
                    //分配
                    pickDetailService.allocate(pickDetail.getId(), pickDetail.getOrderQty() - pickDetail.getAllocatedQty(), pickDetail.getPo(), BaseStatus.ZCCK);
                }
            }
        }
        return new ResponseEntity<>("操作成功",HttpStatus.OK);
    }

    @PostMapping("/qtckAllocate")
    @Log("其他出库分配")
    @ApiOperation("其他出库分配")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> qtckAllocate(@RequestBody Long[] ids){
        for (Long id :ids) {
            List<PickDetail> pickTickets =pickDetailRepository.queryByPickTicketId(id);
            for (PickDetail pickDetail : pickTickets) {
                double unQty=pickDetail.getOrderQty()-pickDetail.getAllocatedQty();
                if(unQty>0) {
                    //分配
                    pickDetailService.allocate(pickDetail.getId(), pickDetail.getOrderQty() - pickDetail.getAllocatedQty(), pickDetail.getPo(), BaseStatus.QTCK);
                }
            }
        }
        return new ResponseEntity<>("操作成功",HttpStatus.OK);
    }
    @PostMapping("/xdckCancelAllocate")
    @Log("箱单出库取消分配")
    @ApiOperation("箱单出库取消分配")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> xdckCancelAllocate(@RequestBody Long[] ids){
        for (Long id :ids) {
            List<PickDetail> pickTickets =pickDetailRepository.queryByPickTicketId(id);
            for (PickDetail pickDetail : pickTickets) {
                if(pickDetail.getAllocatedQty()>0&&pickDetail.getPickedQty()<=0) {
                    //分配
                    pickDetailService.cancelAllocate(pickDetail.getId());
                }
            }
        }
        return new ResponseEntity<>("操作成功",HttpStatus.OK);
    }



    @PostMapping("/pickWhole")
    @Log("备货操作-整单拣货")
    @ApiOperation("备货操作-整单拣货")
    @PreAuthorize("@el.check('super:man')")
    @Transactional
    public ResponseEntity<Object> pickWhole(@RequestBody PickTicketButton resources){
        String bhr=SecurityUtils.getCurrentUsername();
        String stockCode=resources.getStockCode();
        if(resources.getPickConfirmNo()==8){
            List<PickTicket> pts=pickTicketRepository.queryByCusCode(stockCode);
            if(pts.size()<=0){
                throw new BadRequestException("没找到需要下架的箱单");
            }
            resources.setPickTicketId(pts.get(0).getId());
        }
        List<Task> ts=taskRepository.getNotPickTask(resources.getPickTicketId());
        if(ts.size()<=0){
            throw new BadRequestException("没有需要拣货的明细");
        }
        for(Task t:ts) {
            pickTicketService.pickForTask(t.getId(), t.getSrcStockCode(), t.getPlanQty()-t.getMoveQty(), null, bhr);
        }
        return new ResponseEntity<>("操作成功",HttpStatus.OK);
    }

    @PostMapping("/pickWholeApp")
    @Log("箱单出库APP")
    @ApiOperation("箱单出库APP")
    @PreAuthorize("@el.check('super:man')")
    @Transactional
    public ResponseEntity<Object> pickWholeApp(@RequestBody PutawayRequest request){
        String bhr=SecurityUtils.getCurrentUsername();
        String stockCode = request.getStockCode();
        if(stockCode.indexOf("84")==0){
            stockCode=stockCode.substring(2);
        }
        List<PutawayRet> rets=new ArrayList<>();
        if(request.getCheckType()==4){
            List<PickTicket> pts=pickTicketRepository.queryByCusCode(stockCode);
            if(pts.size()<=0){
                throw new BadRequestException("没找到需要下架的箱单");
            }
            Long pickId=pts.get(0).getId();
            List<Task> ts=taskRepository.getNotPickTask(pickId);
            if(ts.size()<=0){
                throw new BadRequestException("没有需要拣货的明细");
            }
            for(Task t:ts) {
                PickDetail pd=t.getPickDetail();
                PutawayRet ret=new PutawayRet();
                ret.createPutawayRet(pickId,pd.getId(),pd.getItem().getCode(),pd.getItem().getName(),t.getPlanQty(),t.getItemKey().getPropC2(),pd.getPo());
                rets.add(ret);
            }
            return new ResponseEntity<>(rets,HttpStatus.OK);

        }else if(request.getCheckType()==6){
            List<PickTicket> pts=pickTicketRepository.queryByCusCode(stockCode);
            if(pts.size()<=0){
                throw new BadRequestException("没找到需要下架的箱单");
            }
            Long pickId=pts.get(0).getId();
            List<Task> ts=taskRepository.getNotPickTask(pickId);
            if(ts.size()<=0){
                throw new BadRequestException("没有需要拣货的明细");
            }
            for(Task t:ts) {
                PickDetail pd=t.getPickDetail();
                PutawayRet ret=new PutawayRet();
                ret.createPutawayRet(pickId,pd.getId(),pd.getItem().getCode(),pd.getItem().getName(),t.getPlanQty(),t.getItemKey().getPropC2(),pd.getPo());
                rets.add(ret);
                pickTicketService.pickForTask(t.getId(), t.getSrcStockCode(), t.getPlanQty()-t.getMoveQty(), null, bhr);
            }
        }

        return new ResponseEntity<>("操作成功",HttpStatus.OK);
    }

    @PostMapping("/xdckPickOut")
    @Log("箱单拣货确认")
    @ApiOperation("箱单拣货确认")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> xdckPickOut(@RequestBody HashMap map){
        Long invId=Long.parseLong(map.get("invId").toString());
        Long detailId=Long.parseLong(map.get("detailId").toString());
        Inventory xdinv =inventoryRepository.getById(invId);
        PickDetail pickDetail=pickDetailRepository.getById(detailId);
        if(pickDetail.getBeXdPf()){
            if(xdinv.getQuantity()<pickDetail.getOrderQty()){
                throw new BadRequestException( "库存数量不足纳入数");
            }
            Task task = taskService.storeTask(null,pickDetail,xdinv.getArea(),xdinv.getItemKey(),
                    xdinv.getPoint(),null,pickDetail.getOrderQty(),xdinv.getStockCode());
            task.setTaskType(BizStatus.XD_PF_PICK);
            taskRepository.save(task);
            pickTicketService.pickForTask(task.getId(), task.getSrcStockCode(), task.getPlanQty()-task.getMoveQty(), null, SecurityUtils.getCurrentUsername());

        }else {
            //所有该箱单库存一次性拣货确认
            List<Inventory> invs = inventoryRepository.queryInvStockCode(xdinv.getStockCode());
            for (Inventory inv : invs) {
                //生成Task，生成出库扣减库存
                Task task = taskService.storeTask(null, pickDetail, inv.getArea(), inv.getItemKey(), inv.getPoint(), null, inv.getQuantity(), inv.getStockCode());
                task.setBillCode(pickDetail.getItem().getCode() + "关联箱单出库" + detailId);
                task.setTaskType(BizStatus.XD_PICK);
                taskRepository.save(task);
                pickTicketService.pickForTask(task.getId(), task.getSrcStockCode(), task.getPlanQty() - task.getMoveQty(), null, SecurityUtils.getCurrentUsername());
            }
        }

        return new ResponseEntity<>("操作成功",HttpStatus.OK);
    }

    @PostMapping("/pickBatch")
    @Log("备货操作-批量拣货")
    @ApiOperation("备货操作-批量拣货")
    @PreAuthorize("@el.check('super:man')")
    @Transactional
    public ResponseEntity<Object> pickBatch(@RequestBody Long[] taskIds){
        String bhr=SecurityUtils.getCurrentUsername();
        for(Long id:taskIds) {
            Task t=taskRepository.getById(id);
            pickTicketService.pickForTask(t.getId(), t.getSrcStockCode(), t.getPlanQty()-t.getMoveQty(), null, bhr);
        }
        return new ResponseEntity<>("操作成功",HttpStatus.OK);
    }

    @PostMapping("/pickSingle")
    @Log("备货操作-单一拣货")
    @ApiOperation("备货操作-单一拣货")
    @PreAuthorize("@el.check('super:man')")
    @Transactional
    public ResponseEntity<Object> pickSingle(@RequestBody PickTicketButton resources){
        String bhr=SecurityUtils.getCurrentUsername();
        Long taskId=resources.getTaskId()[0];
        double pickQty=resources.getOnePickConfirmNumber();
        pickTicketService.pickForTask(taskId, null, pickQty, null, bhr);
        return new ResponseEntity<>("操作成功",HttpStatus.OK);
    }

    @PostMapping("/pickForXpp")
    @Log("备货操作-现品票拣货")
    @ApiOperation("备货操作-现品票拣货")
    @PreAuthorize("@el.check('super:man')")
    @Transactional
    public ResponseEntity<Object> pickForXpp(@RequestBody PickTicketButton resources){
        Long ptId=resources.getPickTicketId();
        String xppQRCode=resources.getXppQRCode();
        //现品票二维码
        XppRecord xpp = xppRecordRepository.findByCode(xppQRCode);
        if (Objects.isNull(xpp)){
            throw new BadRequestException(HttpStatus.NOT_FOUND, "未找到对应的现品票二维码");
        }
        if(xpp.getCkTaskLog()!=null){
            throw new BadRequestException(HttpStatus.NOT_FOUND, "现品票已出库");
        }
        if(!xpp.getStatus().equals(BizStatus.PUTAWAY)){
            throw new BadRequestException(HttpStatus.NOT_FOUND, "现品票状态不正确");
        }
        Task rkT=xpp.getRkTask();
        if(rkT==null){
            throw new BadRequestException(HttpStatus.NOT_FOUND, "现品票未上架");
        }
        ItemKey ik=rkT.getItemKey();
        //取现品票上的库位进行拣货
        Long srcPointId=null;
        if(xpp.getInvPoint()!=null){
            srcPointId=xpp.getInvPoint().getId();
        }
        List<Task> ts=taskService.queryTask(null,null,ik.getId(),ptId,srcPointId);
        Task t=null;
        for(Task task:ts){
            if(task.getMoveQty()<task.getPlanQty()){
                t=task;
            }
        }
        if(ts.size()<=0){
            throw new BadRequestException(HttpStatus.NOT_FOUND, ik.getItem().getCode()+"已全部备货");
        }
        if(t==null){
//            t=ts.get(0);
            throw new BadRequestException(HttpStatus.NOT_FOUND, ik.getItem().getCode()+"找不到待拣货任务");
        }
        String bhr=SecurityUtils.getCurrentUsername();
        pickTicketService.pickForTask(t.getId(),t.getSrcStockCode(),xpp.getNrs(),xpp,bhr);
        return new ResponseEntity<>("操作成功",HttpStatus.OK);
    }

    @PostMapping("/xppPickGoods")
    @Log("App现品票拣货")
    @ApiOperation("App现品票拣货")
    @Transactional
    @DuplicateSubmission
    @AnonymousAccess
    public ResponseEntity<Object> xppPickGoods(@RequestBody PickTicketButton resources){
        //现品票二维码
        String xppQRCode = resources.getXppQRCode();
        XppRecord xpp = xppRecordRepository.findByCode(xppQRCode);
        if (Objects.isNull(xpp)){
            throw new BadRequestException(HttpStatus.NOT_FOUND, "未找到对应的现品票二维码");
        }
        if(xpp.getCkTaskLog()!=null){
            throw new BadRequestException(HttpStatus.NOT_FOUND, "现品票已出库");
        }
        Task rkT=xpp.getRkTask();
        if(rkT==null){
            throw new BadRequestException(HttpStatus.NOT_FOUND, "现品票未上架");
        }
        ItemKey ik=rkT.getItemKey();
        //取现品票上的库位进行拣货
        Long srcPointId=null;
        if(xpp.getInvPoint()!=null){
            srcPointId=xpp.getInvPoint().getId();
        }
        List<Task> ts=taskService.queryTask(null,null,ik.getId(),resources.getPickTicketId(),srcPointId);
        Task t=null;
        for(Task task:ts){
            if(task.getMoveQty()<task.getPlanQty()){
                t=task;
            }
        }
        if(ts.size()<=0){
            throw new BadRequestException(HttpStatus.NOT_FOUND, ik.getItem().getCode()+"已全部备货");
        }
        if(t==null){
//            t=ts.get(0);
            throw new BadRequestException(HttpStatus.NOT_FOUND, ik.getItem().getCode()+"找不到待拣货任务");
        }
        pickTicketService.pickForTask(t.getId(),resources.getStockCode(),xpp.getNrs(),xpp,resources.getBhr());
        HashMap<String,Object> map=new HashMap<>();
        Item it=ik.getItem();
        PickTicket pt=t.getPickDetail().getPickTicket();
        map.put("itemCode",it.getCode());
        map.put("itemName",it.getName());
        map.put("dds",pt.getOrderQuantity());
        map.put("bhs",pt.getPickedQuantity());
        map.put("ddbh",xpp.getDdbh());
        map.put("bcsl",xpp.getNrs());
        map.put("fzh",xpp.getFzh());
        map.put("tjkw",t.getSrcPoint().getCode());
        return new ResponseEntity<>(map,HttpStatus.OK);
    }

    @PostMapping("/queryTaskLogDataS")
    @Log("拣货取消的数据查询")
    @ApiOperation("拣货取消的数据查询")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<List<TaskLog>> queryTaskLogDataS(@RequestBody PickTicketButton resources){
        return new ResponseEntity<>(pickTicketService.queryTaskLogDataS(resources.getPickTicketId()),HttpStatus.CREATED);
    }

    @PostMapping("/pickingCancel")
    @Log("拣货取消")
    @ApiOperation("拣货取消")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> pickingCancel(@RequestBody PickTicketButton resources){
        pickTicketService.pickingCancel(resources);
        return new ResponseEntity<>("拣货取消成功",HttpStatus.CREATED);
    }

    @PostMapping("/xbjsVerify")
    @Log("线边接收确认")
    @ApiOperation("线边接收确认")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> xbjsVerify(@RequestBody Long[] ids) {
        for (Long id : ids) {
            PickTicket pt=pickTicketRepository.getById(id);
            List<TaskLog> logs=taskLogRepository.getTaskLogsByPtId(id);
            double shNum = 0d;
            for (TaskLog log : logs) {
                log.setSlQty(log.getShipQty());
                log.setSlBy(SecurityUtils.getCurrentUsername());
                taskLogService.update(log);
                shNum+=log.getSlQty();
                //生成具体的库存信息
                Task task=log.getTask();
                PickDetail pickDetail=task.getPickDetail();
//                Point point=pt.getShArea().getPoint();
                Point point=pointService.findEntityById(pt.getShArea().getPointId());
                Dept dept= UserUtils.getDept();
                //线边接收不需要管理到批次，重新生成itemKey
                ItemKey itemKey=itemKeyService.getItemKey(log.getItemKey().getItem(),null,task.getItemKey().getPropC2());
                Inventory inventory=inventoryService.getInventory(itemKey,pt.getShArea(),point,pickDetail.getPoint(),dept,BizStatus.SL_ALL,null);
                double srcQty=inventory.getQuantity();
                inventory.setQuantity(inventory.getQuantity()+log.getSlQty());
                inventoryService.update(inventory);
                inventoryLogService.storeInventoryLog(BizStatus.SL_ALL, BizStatus.ADD, null, pt.getShArea(), log.getItemKey(), inventory.getPoint(), inventory.getPoint(), null,null, srcQty, log.getSlQty(),null,log.getSrcStockCode(),
                        BizStatus.SL_ALL, log.getId(), inventory.getId(), "线边接收");
            }
            pt.setSlQty(shNum);
            pt.setSlBy(SecurityUtils.getCurrentUsername());
            pt.setSlDate(new Timestamp(new Date().getTime()));
            pt.setStatus(BizStatus.SL_ALL);
            pickTicketService.update(pt);

        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/shipmentRegistration")
    @Log("发货登记")
    @ApiOperation("发货登记")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> shipmentRegistration(@RequestBody Long id) {
        pickTicketService.shipmentRegistration(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/queryDetails")
    @Log("根据pickTicket的id查询详情数据")
    @ApiOperation("根据pickTicket的id查询详情数据")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> queryDetails(@RequestBody Long id){
        Map<String,Object> map = new HashMap<>();
        PickTicketDto pickTicketDto = pickTicketService.findById(id);
        //查询出库明细
        map.put("pickDetailList",pickDetailRepository.queryByPickTicketId(id));
        //查询分配记录
        map.put("pickDetailAllocateList",taskRepository.queryByBillCode(pickTicketDto.getCode()));
        //查询拣货记录
        map.put("pickDetailPickingList",taskLogRepository.getTaskLogsByPtId(id));
        return new ResponseEntity<>(map,HttpStatus.OK);
    }

    @PostMapping("/pickStatusUpdate")
    @Log("备货单状态更新")
    @ApiOperation("备货单状态更新")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> pickStatusUpdate(@RequestBody Long[] ids) {
        for (Long id : ids) {
            PickTicket pt=pickTicketRepository.getById(id);
            List<PickDetail> ptds=pickDetailRepository.queryByPickTicketId(id);
            if(pt.getSlQty()>0){
                break;
            }
            double orderQty=0;
            double cdQty=0;
            double pickQty=0;
            double shipQty=0;
            for(PickDetail ptd:ptds){
                orderQty+=ptd.getOrderQty();
                List<Task> ts=taskRepository.getPickDetailTasks(ptd.getId());
                double t_cd_qty=0;
                double t_pick_qty=0;
                double t_ship_qty=0;
                for(Task t:ts){
                    t_cd_qty+=t.getPlanQty();
                    List<TaskLog> logs=taskLogRepository.getTaskLogbyTaskId(t.getId());
                    for(TaskLog log:logs){
                        t_pick_qty+=log.getMoveQty();
                        t_ship_qty+=log.getShipQty();
                    }
                }
                ptd.setAllocatedQty(t_cd_qty);
                ptd.setPickedQty(t_pick_qty);
                ptd.setShippedQty(t_ship_qty);
                cdQty+=ptd.getAllocatedQty();
                pickQty+=ptd.getPickedQty();
                shipQty+=ptd.getShippedQty();
                pickDetailService.update(ptd);
            }
            pt.setOrderQuantity(orderQty);
            pt.setAllocatedQuantity(cdQty);
            pt.setPickedQuantity(pickQty);
            pt.setShippedQuantity(shipQty);
            if(cdQty>0){
                pt.setStatus(BizStatus.ALLOCATE);
            }
            if(pickQty>0&&pickQty<cdQty){
                pt.setStatus(BizStatus.PICKUP);
            }
            if(pickQty>0&&pickQty>=cdQty){
                pt.setStatus(BizStatus.PICK_ALL);
            }
            if(shipQty>0&&shipQty<pickQty){
                pt.setStatus(BizStatus.SHIP_PART);
            }
            if(shipQty>0&&shipQty==pickQty){
                pt.setStatus(BizStatus.SHIP_ALL);
            }
            pickTicketService.update(pt);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

}