package com.gree.mall.miniapp.controller.material.parts;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.miniapp.bean.inf.ValidGroup;
import com.gree.mall.miniapp.bean.material.parts.NewRefundManageBean;
import com.gree.mall.miniapp.bean.material.parts.NewRefundManageItemBean;
import com.gree.mall.miniapp.constant.Constant;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.logic.material.parts.NewRefundManageLogic;
import com.gree.mall.miniapp.utils.ValidateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ValidationException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "新件返还管理", tags ={"新件返还管理"} )
@RequestMapping(value = "/app/worker/new-refund", produces = "application/json; charset=utf-8")
public class NewRefundController {

    @Resource
    NewRefundManageLogic newRefundManageLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

    @ApiOperation(value = "新件返还管理列表")
    @PostMapping("/list")
    public ResponseHelper<IPage<NewRefundManageBean>> appList(
            @ApiParam(value = "申请单号") @RequestParam(required = false) String applyNo,
            @ApiParam(value = "单据状态 SAVE=保存 SUBMIT=提交 AGREE=通过 REJECT=驳回 REFUNDED=已返还") @RequestParam(required = false) String flag,
            @ApiParam(value = "师傅身份证", required = true) @RequestParam String identity,
            @ApiParam(value = "配件编码") @RequestParam(required = false) String partsNumber,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNo,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws RemoteServiceException {
        return ResponseHelper.success(newRefundManageLogic.appList(applyNo, flag, identity, partsNumber, pageNo, pageSize));
    }

    @ApiOperation(value = "新件返还管理详情")
    @PostMapping("/detail")
    public ResponseHelper<NewRefundManageBean> detail(
            @ApiParam(value = "申请单号",required = true) @RequestParam String applyNo
    ) throws RemoteServiceException {
        return ResponseHelper.success(newRefundManageLogic.detail(applyNo));
    }

    @ApiOperation(value = "新件返还申请")
    @PostMapping("/add")
    public ResponseHelper add(
            @RequestBody NewRefundManageBean newRefundManageBean
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.PARTS_SALES + newRefundManageBean.getSalesId());
        if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("销售单冻结中");
        }
        try {
            // 校验
            ValidateUtil.validate(newRefundManageBean, ValidGroup.Add.class);
            for (NewRefundManageItemBean itemBean : newRefundManageBean.getNewRefundManageItemBeanList()) {
                try {
                    ValidateUtil.validate(itemBean, ValidGroup.Add.class);
                } catch (ValidationException e) {
                    throw new RemoteServiceException("“" +itemBean.getPartsName() + "”出错原因：" + e.getMessage());
                }
            }
            newRefundManageLogic.add(newRefundManageBean);
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }
}
