package com.sy.coinage.workshop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sy.coinage.core.annotation.CurrentUser;
import com.sy.coinage.core.annotation.LoginNotRequired;
import com.sy.coinage.core.controller.BaseController;
import com.sy.coinage.core.req.IdReq;
import com.sy.coinage.core.util.DateUtils;
import com.sy.coinage.core.vo.CacheUserVO;
import com.sy.coinage.sys.model.Dept;
import com.sy.coinage.sys.model.Dict;
import com.sy.coinage.sys.service.IDeptService;
import com.sy.coinage.sys.service.IDictService;
import com.sy.coinage.workshop.model.DeliveryDetail;
import com.sy.coinage.workshop.model.WorkshopEntryDetail;
import com.sy.coinage.workshop.model.WorkshopOutboundDetail;
import com.sy.coinage.workshop.model.WorkshopTransferDetail;
import com.sy.coinage.workshop.req.WareReq;
import com.sy.coinage.workshop.service.*;
import com.sy.coinage.workshop.vo.ViewInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * -rfid基础信息登记表Controller
 */
@Api(value = "rfid基础信息登记表接口", tags = {"rfid基础信息登记表接口"})
@RestController
@RequestMapping(value = "/view")
public class ViewController extends BaseController {

    @Autowired
    private IDictService dictService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private IDeliveryDetailService deliveryDetailService;

    @Autowired
    private IWorkshopEntryDetailService workshopEntryDetailService;

    @Autowired
    private IWorkshopOutboundDetailService workshopOutboundDetailService;

    @Autowired
    private IWorkshopTransferDetailService workshopTransferDetailService;


    @ApiOperation(value = "车间临时库大屏初始信息列表")
    @LoginNotRequired
    @PostMapping("/shop/list")
    public List<ViewInfoVO> shopList(@RequestBody @Valid IdReq req) {
        List<ViewInfoVO> data = new ArrayList<>();
        Dict dict = this.dictService.getByVal("locationOpt", req.getId(), null);
        Dept dept = this.deptService.getByCode(req.getId());
        Date d = new Date();
        List<WorkshopEntryDetail> l = this.getWorkshopEntryDetail(d, dept.getId());
        for (WorkshopEntryDetail detail : l) {
            ViewInfoVO vo = new ViewInfoVO();
            BeanUtils.copyProperties(detail, vo);
            // 2入库3:出库
            vo.setInOrOut(2);
            vo.setStatus(null);
            //  "-1复核异常 1解缴待入库 2解缴中 3解缴已入库 5已发货"
            vo.setOperateTime(detail.getCreateTime());
            vo.setCreateBy(detail.getOperatorCn());
            vo.setLocWare(dict.getName());
            vo.setDept(dept.getName());
            //todo 将称重时间、称重日期写入
//            vo.setWeightDate(detail.getWeightDate()); //detail中weightTime格式：yyyy-mm-dd hh:mm:ss
//            vo.setWeightTime(detail.getWeightTime());
//            String date = DateUtils.toString(detail.getWeightDate()) + " " + detail.getWeightTime();
            vo.setPrintTime(detail.getWeightTime());
            data.add(vo);
        }
        List<WorkshopOutboundDetail> ll = this.getShopOutboundDetail(d, dept.getId());
        for (WorkshopOutboundDetail detail : ll) {
            ViewInfoVO vo = new ViewInfoVO();
            BeanUtils.copyProperties(detail, vo);
            // 2入库3:出库
            if (detail.getOrderType().intValue() == 1) {
                vo.setInOrOut(3);
                if (detail.getInTime() != null) {
                    //  "-1复核异常 1入库 2解缴出库 3解缴已入库 4返检待入库 5返检已入库 6内部出库"
                    vo.setStatus(3);
                    vo.setOperateTime(detail.getInTime());
                    vo.setLocWare(dict.getName());
                } else {
                    vo.setStatus(2);
                    vo.setOperateTime(detail.getOutTime());
                }
            } else {
                vo.setInOrOut(2);
                if (detail.getInTime() != null) {
                    //  "-1复核异常 1入库 2解缴出库 3解缴已入库 4返检待入库 5返检已入库 6内部出库"
                    vo.setStatus(5);
                    vo.setOperateTime(detail.getInTime());
                    vo.setLocWare(dict.getName());
                } else {
                    vo.setStatus(4);
                    vo.setOperateTime(detail.getOutTime());
                }
            }

            String date = DateUtils.toString(detail.getWeightDate()) + " " + detail.getWeightTime();
            vo.setPrintTime(DateUtils.toDate(date, DateUtils.YY_MM_DD_HH_mm_ss));
            data.add(vo);
        }
        List<WorkshopTransferDetail> lll = this.getWorkshopTransferDetail(d, dept.getId());
        for (WorkshopTransferDetail detail : lll) {
            ViewInfoVO vo = new ViewInfoVO();
            BeanUtils.copyProperties(detail, vo);
            // 2入库3:出库
            vo.setInOrOut(3);
            vo.setStatus(6);
            // ：临时库-1复核异常 1入库 2解缴出库 3解缴已入库 4返检待入库 5返检已入库 6内部出库
            vo.setOperateTime(detail.getCreateTime());
            vo.setCreateBy(detail.getOperatorCn());
            vo.setLocWare(dict.getName());
            vo.setDept(dept.getName());
            //todo 将称重时间、称重日期写入
            String date = DateUtils.toString(detail.getWeightDate()) + " " + detail.getWeightTime();
            vo.setPrintTime(DateUtils.toDate(date, DateUtils.YY_MM_DD_HH_mm_ss));
//            vo.setWeightDate(detail.getWeightDate()); //detail中weightTime格式：yyyy-mm-dd hh:mm:ss
//            vo.setWeightTime(detail.getWeightTime());
            data.add(vo);
        }
        data.sort((o1, o2) -> {
            boolean b = o1.getOperateTime().before(o2.getOperateTime());
            if (b) {
                return 1;
            }
            return -1;
        });
        return data;
    }

    private List<WorkshopEntryDetail> getWorkshopEntryDetail(Date d, Integer deptId) {
        QueryWrapper<WorkshopEntryDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rfid_workshop_entry.dept_id", deptId)
                .eq("rfid_workshop_entry_detail.type", 1);
        queryWrapper.between("rfid_workshop_entry.create_time", DateUtils.getDayStartTime(d),
                DateUtils.getDayEndTime(d));
        return this.workshopEntryDetailService.querylist(queryWrapper);
    }

    private List<WorkshopTransferDetail> getWorkshopTransferDetail(Date d, Integer deptId) {
        QueryWrapper<WorkshopTransferDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rfid_workshop_transfer.dept_id", deptId)
                .eq("rfid_workshop_transfer.op_type", 2)
                .gt("rfid_workshop_transfer.racket_num", 0);
        queryWrapper.between("rfid_workshop_transfer.create_time", DateUtils.getDayStartTime(d),
                DateUtils.getDayEndTime(d));
        return this.workshopTransferDetailService.querylist(queryWrapper);
    }

    @ApiOperation(value = "主库大屏初始信息列表")
    @LoginNotRequired
    @PostMapping("/ware/list")
    public List<ViewInfoVO> wareList(@RequestBody WareReq req) {
        List<ViewInfoVO> data = new ArrayList<>();
        Dict dict = this.dictService.getByVal("locationOpt", req.getWareId(), null);
        Date d = new Date();
        List<DeliveryDetail> l = this.getDeliveryDetail(d, req.getWareId());
        for (DeliveryDetail detail : l) {
            ViewInfoVO vo = new ViewInfoVO();
            BeanUtils.copyProperties(detail, vo);
            // 2入库3:出库
            vo.setInOrOut(3);
            //  "-1复核异常 1解缴待入库 2解缴中 3解缴已入库 5已发货 6返检出库 7返检车间入库"
            vo.setStatus(5);
            vo.setOperateTime(detail.getDeliveryTime());
            vo.setCreateBy(detail.getOperator());
            vo.setLocWare(dict.getName());
            //todo 发货登记需要将打印时间赋值
            vo.setPrintTime(detail.getPrintTime());
            data.add(vo);
        }
        List<WorkshopOutboundDetail> ll = this.getWorkshopOutboundDetail(d, req.getWareId());
        for (WorkshopOutboundDetail detail : ll) {
            ViewInfoVO vo = new ViewInfoVO();
            BeanUtils.copyProperties(detail, vo);
            if (detail.getOrderType().intValue() == 1) {
                if (detail.getInTime() != null) {
                    // 2入库3:出库
                    vo.setInOrOut(2);
                    //  "-1复核异常 1解缴待入库 2解缴中 3解缴已入库 5已发货 6返检出库 7返检车间入库"
                    vo.setStatus(3);
                    vo.setOperateTime(detail.getInTime());
                    vo.setLocWare(dict.getName());
                } else {
                    // 2入库3:出库
//                vo.setInOrOut(2);
                    //  "-1复核异常 1解缴待入库 2解缴中 3解缴已入库 5已发货 6返检出库 7返检车间入库"
                    vo.setStatus(1);
                    vo.setOperateTime(detail.getOutTime());
                }
            } else {
                if (detail.getInTime() != null) {
                    // 2入库3:出库
                    vo.setInOrOut(3);
                    //  "-1复核异常 1解缴待入库 2解缴中 3解缴已入库 5已发货 6返检出库 7返检车间入库"
                    vo.setStatus(7);
                    vo.setOperateTime(detail.getOutTime());
                    vo.setLocWare(dict.getName());
                } else {
                    vo.setInOrOut(3);
                    // 2入库3:出库
//                vo.setInOrOut(2);
                    //  "-1复核异常 1解缴待入库 2解缴中 3解缴已入库 5已发货 6返检出库 7返检车间入库"
                    vo.setStatus(6);
                    vo.setOperateTime(detail.getOutTime());
                }
            }

            String date = DateUtils.toString(detail.getWeightDate()) + " " + detail.getWeightTime();
            vo.setPrintTime(DateUtils.toDate(date, DateUtils.YY_MM_DD_HH_mm_ss));
            data.add(vo);
        }
        data.sort((o1, o2) -> {
            boolean b = o1.getOperateTime().before(o2.getOperateTime());
            if (b) {
                return 1;
            }
            return -1;
        });
        return data;
    }

    private List<DeliveryDetail> getDeliveryDetail(Date d, Integer wareId) {
        QueryWrapper<DeliveryDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rfid_delivery_detail.ware_id", wareId)
                .between("rfid_delivery_detail.delivery_time", DateUtils.getDayStartTime(d), DateUtils.getDayEndTime(d));
        return this.deliveryDetailService.queryList(queryWrapper);
    }

    private List<WorkshopOutboundDetail> getWorkshopOutboundDetail(Date d, Integer wareId) {
        QueryWrapper<WorkshopOutboundDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(wq -> {
            wq.eq("order_type", 1).eq("rfid_workshop_outbound.status", 2).isNull("in_time")
                    .between("out_time", DateUtils.getDayStartTime(d), DateUtils.getDayEndTime(d))
                    .or().eq("ware_id", wareId).between("out_time", DateUtils.getDayStartTime(d), DateUtils.getDayEndTime(d));
        });
        return this.workshopOutboundDetailService.querylist(queryWrapper);
    }

    private List<WorkshopOutboundDetail> getShopOutboundDetail(Date d, Integer deptId) {
        QueryWrapper<WorkshopOutboundDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rfid_workshop_outbound.dept_id", deptId)
                .between("out_time", DateUtils.getDayStartTime(d), DateUtils.getDayEndTime(d));
        queryWrapper.and(wq -> {
            wq.eq("rfid_workshop_outbound.status", 2).or().eq("rfid_workshop_outbound.status", 3);
        });
        return this.workshopOutboundDetailService.querylist(queryWrapper);
    }

}
