package com.sy.coinage.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sy.coinage.app.req.WareConfirmReq;
import com.sy.coinage.core.annotation.CurrentUser;
import com.sy.coinage.core.controller.BaseController;
import com.sy.coinage.core.exception.RetException;
import com.sy.coinage.core.vo.CacheUserVO;
import com.sy.coinage.sys.model.Dept;
import com.sy.coinage.sys.service.IDeptService;
import com.sy.coinage.workshop.model.WorkshopOutbound;
import com.sy.coinage.workshop.model.WorkshopOutboundDetail;
import com.sy.coinage.workshop.req.OrderNoReq;
import com.sy.coinage.workshop.service.IWorkshopOutboundDetailService;
import com.sy.coinage.workshop.service.IWorkshopOutboundService;
import com.sy.coinage.workshop.vo.WorkshopOutboundVO;
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.List;

/**
 * -车间出库单据（返检单据|）Controller
 */
@Api(tags = "App-车间出库单据（返检单据|）接口")
@RestController
@RequestMapping(value = "/app/returnCheck")
public class AppReturnCheckController extends BaseController {

    @Autowired
    private IDeptService deptService;

    @Autowired
    private IWorkshopOutboundService workshopOutboundService;

    @Autowired
    private IWorkshopOutboundDetailService workshopOutboundDetailService;


    // ===========返检登记
//    @ApiOperation(value = "4.0 app获取所有返检单号")
//    @GetMapping("/list")
//    public List<Object> returnCheckList(@CurrentUser CacheUserVO user) {
//        LambdaQueryWrapper<WorkshopOutbound> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.select(WorkshopOutbound::getOrderNo);
//        queryWrapper.eq(WorkshopOutbound::getDeptId, user.getDeptId());
//        // 单据类型： 1: 解缴单  2: 返检单
//        queryWrapper.eq(WorkshopOutbound::getOrderType, 2);
//        // 状态:1.新建 2.已出库 3.已入库
//        queryWrapper.eq(WorkshopOutbound::getStatus, 1);
//        List<Object> data = this.workshopOutboundService.listObjs(queryWrapper);
//        return data;
//    }

    // ===========返检登记
    @ApiOperation(value = "4.0-new app获取所有返检单号")
    @GetMapping("/list/{wareId}")
    public List<Object> returnCheckList1(@PathVariable Integer wareId) {
        Dept dept = null;
        if (wareId < 200) {
            dept = this.deptService.getByCode(wareId);
        }
        LambdaQueryWrapper<WorkshopOutbound> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(WorkshopOutbound::getOrderNo);
        if (dept != null) {
            queryWrapper.eq(WorkshopOutbound::getDeptId, dept.getId());
        }
        // 单据类型： 1: 解缴单  2: 返检单
        queryWrapper.eq(WorkshopOutbound::getOrderType, 2);
        // 状态:1.新建 2.已出库 3.已入库
        queryWrapper.eq(WorkshopOutbound::getStatus, 1);
        List<Object> data = this.workshopOutboundService.listObjs(queryWrapper);
        return data;
    }

    @ApiOperation(value = "4.1 app根据返检单号，获取返检基本信息接口")
    @PostMapping("/getInfoByOrderNo")
    public WorkshopOutboundVO getInfoByOrderNo(@RequestBody @Valid OrderNoReq req) {
        if (req.getOpFlag() == null || req.getOpFlag().intValue() == 0) {
            throw new RetException("返检操作标识不能为空");
        }
        WorkshopOutboundVO data = new WorkshopOutboundVO();
        LambdaQueryWrapper<WorkshopOutbound> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(WorkshopOutbound::getDeptId, user.getDeptId());
        // 单据类型： 1: 解缴单  2: 返检单
        queryWrapper.eq(WorkshopOutbound::getOrderType, 2);
        // 单号
        queryWrapper.eq(WorkshopOutbound::getOrderNo, req.getOrderNo());
        // 返检操作标识 1:主库返检出库 2:车间返检入库
        if (req.getOpFlag() == 1) {
            // 状态:1.新建 2.已出库 3.已入库
            queryWrapper.eq(WorkshopOutbound::getStatus, 1);
        } else {
            // 状态:1.新建 2.已出库 3.已入库
            queryWrapper.eq(WorkshopOutbound::getStatus, 2);
        }
        WorkshopOutbound model = this.workshopOutboundService.getOne(queryWrapper);
        BeanUtils.copyProperties(model, data);
        List<WorkshopOutboundDetail> list = workshopOutboundDetailService.list(new LambdaQueryWrapper<WorkshopOutboundDetail>().eq(WorkshopOutboundDetail::getOutId, model.getId()));
        data.setDetailList(list);
        return data;
    }

    @ApiOperation(value = "4.2 app主库确认返检登记（返检出库）接口")
    @PostMapping("/wareConfirm")
    public Object wareConfirm(@RequestBody @Valid WareConfirmReq req, @CurrentUser CacheUserVO user) {
        this.workshopOutboundService.returnCheckWareOut(req, user);
        return "";
    }

    // ===========返检红冲
//    @ApiOperation(value = "5.0 app获取所有待入库返检单号")
//    @GetMapping("/listByShop")
//    public List<Object> returnCheckListByShop(@CurrentUser CacheUserVO user) {
//        LambdaQueryWrapper<WorkshopOutbound> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.select(WorkshopOutbound::getOrderNo);
//        queryWrapper.eq(WorkshopOutbound::getDeptId, user.getDeptId());
//        // 单据类型： 1: 解缴单  2: 返检单
//        queryWrapper.eq(WorkshopOutbound::getOrderType, 2);
//        // 状态:1.新建 2.已出库 3.已入库
//        queryWrapper.eq(WorkshopOutbound::getStatus, 2);
//        List<Object> data = this.workshopOutboundService.listObjs(queryWrapper);
//        return data;
//    }

    @ApiOperation(value = "5.0-new app获取所有待入库返检单号")
    @GetMapping("/listByShop/{wareId}")
    public List<Object> returnCheckListByShop1(@PathVariable Integer wareId) {
        Dept dept = null;
        if (wareId < 200) {
            dept = this.deptService.getByCode(wareId);
        }
        LambdaQueryWrapper<WorkshopOutbound> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(WorkshopOutbound::getOrderNo);
        if (dept != null) {
            queryWrapper.eq(WorkshopOutbound::getDeptId, dept.getId());
        }
        // 单据类型： 1: 解缴单  2: 返检单
        queryWrapper.eq(WorkshopOutbound::getOrderType, 2);
        // 状态:1.新建 2.已出库 3.已入库
        queryWrapper.eq(WorkshopOutbound::getStatus, 2);
        List<Object> data = this.workshopOutboundService.listObjs(queryWrapper);
        return data;
    }

    @ApiOperation(value = "5.2 app车间确认返检产品进入车间临时库")
    @PostMapping("/shopConfirm")
    public Object shopConfirm(@RequestBody @Valid WareConfirmReq req, @CurrentUser CacheUserVO user) {
        this.workshopOutboundService.returnCheckShopIn(req, user);
        return "";
    }

}
