package com.sy.coinage.workshop.controller;

import com.sy.coinage.app.req.SerialNumberReq;
import com.sy.coinage.core.exception.RetException;
import com.sy.coinage.workshop.service.IBaseInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sy.coinage.core.annotation.ActiveFastJsonProfile;
import com.sy.coinage.core.annotation.CurrentUser;
import com.sy.coinage.core.aop.annotation.Permission;
import com.sy.coinage.core.aop.annotation.Operation;
import com.sy.coinage.core.controller.BaseController;
import com.sy.coinage.core.interceptor.Create;
import com.sy.coinage.core.interceptor.Update;
import com.sy.coinage.core.req.*;
import com.sy.coinage.core.vo.*;

import com.sy.coinage.workshop.model.*;
import com.sy.coinage.workshop.service.ILogisticsService;
import com.sy.coinage.workshop.vo.*;
import com.sy.coinage.workshop.req.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
 * -入库表主表Controller
 */
@Api(value = "入库表主表接口", tags = {"入库表主表接口"})
@RestController
@RequestMapping(value = "/logistics")
public class LogisticsController extends BaseController {

    @Autowired
    private ILogisticsService logisticsService;

    @Autowired
    private IBaseInfoService baseInfoService;

    @ApiOperation(value = "生命周期详情列表")
    @PostMapping("/list")
    public List<Logistics> list(@RequestBody @Valid SerialNumberReq req, @CurrentUser CacheUserVO user) {
        LambdaQueryWrapper<Logistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Logistics::getSerialNumber, req.getSerialNumber());
        queryWrapper.orderByAsc(Logistics::getCreateTime);
        List<Logistics> data = this.logisticsService.list(queryWrapper);
        return data;
    }

    @ApiOperation(value = "单箱生命周期")
    @PostMapping("/one")
    public List<Logistics> list(@RequestBody @Valid LogisticsListReq req, @CurrentUser CacheUserVO user) {
        BaseInfo info = this.baseInfoService.getByCrown(req.getSecuritiesId(), req.getCrown());
        if (info == null) {
            throw new RetException("未查询到相关的冠字号");
        }
        LambdaQueryWrapper<Logistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(qw -> {
            qw.eq(Logistics::getRfid, info.getRfid()).isNull(Logistics::getCrownBetween)
                    .or().like(Logistics::getCrownBetween, req.getCrown());
        });
        queryWrapper.orderByDesc(Logistics::getCreateTime);
        List<Logistics> data = this.logisticsService.list(queryWrapper);
        return data;
    }

}
