package com.sy.coinage.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sy.coinage.app.req.BindReq;
import com.sy.coinage.app.req.HalfBindReq;
import com.sy.coinage.app.req.SerialNumberReq;
import com.sy.coinage.core.annotation.ActiveFastJsonProfile;
import com.sy.coinage.core.annotation.CurrentUser;
import com.sy.coinage.core.controller.BaseController;
import com.sy.coinage.core.redis.RedisService;
import com.sy.coinage.core.vo.CacheUserVO;
import com.sy.coinage.workshop.model.BaseInfo;
import com.sy.coinage.workshop.req.RfidReq;
import com.sy.coinage.workshop.service.IBaseInfoService;
import com.sy.coinage.workshop.vo.BaseInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.validation.Valid;

/**
 * -rfid基础信息登记表Controller
 */
@Api(tags = "APP-rfid基础信息登记表接口")
@RestController
@RequestMapping(value = "/app/baseInfo")
public class AppBaseInfoController extends BaseController {

    @Autowired
    private IBaseInfoService baseInfoService;

    @Autowired
    private RedisService redisService;

    private String creadeOutOrderNo(String securities, Integer deptId) {
        // 车间出库单号由系统自动生成，编号由“品名+部门编号+T2+3位数字编号”构成，Y211-1T2001
        // 车间出库单号
        String resp = null;
        String key = "workshopout_num:" + deptId + ":" + securities;
        if (redisService.hasKey(key)) {
            resp = securities + "-" + deptId + "T2" + String.format("%03d",
                Integer.parseInt(this.redisService.get(key)) + 1);
        } else {
            this.redisService.set(key, "0");
            resp = securities + "-" + deptId + "T2" + String.format("%03d", 1);
        }
        return resp;
    }

    @ApiOperation(value = "2.2 app根据产品rfid获取物料基本信息")
    @PostMapping("/getByRfid")
    @ActiveFastJsonProfile(groups = "get", clazz = BaseInfo.class)
    public BaseInfoVO getByRfid(@RequestBody @ApiParam(value = "主键请求bean") @Valid RfidReq req) {
        BaseInfoVO data = null;
        LambdaQueryWrapper<BaseInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseInfo::getRfid, req.getRfid());
        BaseInfo model = this.baseInfoService.getOne(queryWrapper);
        data = new BaseInfoVO();
        BeanUtils.copyProperties(model, data);
        if (req.getIsNeedOrderNo() != null && req.getIsNeedOrderNo()) {
            // FIXME 生成解缴单号
            data.setOrderNo(this.creadeOutOrderNo(model.getSecurities(), model.getDeptId()));
        }
        return data;
    }

    @ApiOperation(value = "10.1.2 app拆拍登记")
    @PostMapping("/halfBind")
    @ActiveFastJsonProfile(groups = "get", clazz = BaseInfo.class)
    public Object halfBind(@RequestBody @ApiParam(value = "主键请求bean") @Valid HalfBindReq req, @CurrentUser CacheUserVO user) {
        this.baseInfoService.halfBind(req, user);
        return "";
    }

    @ApiOperation(value = "10.2.1 app整拍登记时查询详情信息")
    @PostMapping("/getBySerialNumber")
    public BaseInfoVO getBySerialNumber(@RequestBody @ApiParam(value = "主键请求bean") @Valid SerialNumberReq req) {
        BaseInfoVO data = null;
        LambdaQueryWrapper<BaseInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseInfo::getSerialNumber, req.getSerialNumber());
        queryWrapper.eq(BaseInfo::getStatus, 1);
        queryWrapper.eq(BaseInfo::getOriginType, 1);
        //todo 原有从封包线数据来源的数据已经绑定了rfid. 那么整拍登记实际上是更新新的rfid标签。因此是更新该rfid数据
//        queryWrapper.isNull(BaseInfo::getRfid);
        BaseInfo model = this.baseInfoService.getOne(queryWrapper);
        if (model == null) {
            // FIXME 调用第三方接口获取信息并缓存
        }
        data = new BaseInfoVO();
        BeanUtils.copyProperties(model, data);
        return data;
    }

    @ApiOperation(value = "10.2.2 app整拍登记")
    @PostMapping("/bind")
    public Object bind(@RequestBody @ApiParam(value = "主键请求bean") @Valid BindReq req, @CurrentUser CacheUserVO user) {
        this.baseInfoService.bind(req, user);
        return "";
    }

}
