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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.miniapp.bean.material.parts.OldRefundManageBean;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.logic.material.parts.OldRefundManageLogic;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

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

    @Resource
    OldRefundManageLogic oldRefundManageLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;


    @ApiOperation(value = "旧件返还管理列表")
    @PostMapping("/list")
    public ResponseHelper<IPage<OldRefundManageBean>> appList(
            @ApiParam(value = "申请单号") @RequestParam(required = false) String applyNo,
            @ApiParam(value = "维修标识 INNER=保内 OUTSIDE=保外") @RequestParam(required = false) String repairFlag,
            @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(oldRefundManageLogic.appList(applyNo, repairFlag, flag, identity, partsNumber, pageNo, pageSize));
    }

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


//    @ApiOperation(value = "PHP旧件返还申请")
//    @PostMapping("/add")
//    public ResponseHelper add(
//            @RequestBody PHPOldRefundManageBean oldRefundManageBean
//    ) throws Exception {
//        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.NEW_REFUND + "add");
//        try {
//            if (obtain.tryLock(10, TimeUnit.SECONDS)) {
//                // 校验
//                ValidateUtil.validate(oldRefundManageBean, ValidGroup.Add.class);
//                for (PHPOldRefundManageItemBean itemBean : oldRefundManageBean.getOldRefundManageItemBeanList()) {
//                    try {
//                        ValidateUtil.validate(itemBean, ValidGroup.Add.class);
//                    } catch (ValidationException e) {
//                        throw new RemoteServiceException("明细出错原因：" + e.getMessage());
//                    }
//                }
//
//                return ResponseHelper.success(oldRefundManageLogic.add(oldRefundManageBean));
//            }
//        } catch(Exception e) {
//            log.error("【旧件返还提交处理】失败",e);
//            throw e;
//        } finally {
//            obtain.unlock();
//        }
//        return ResponseHelper.error("");
//    }

//    @PostMapping("/add")
//    @ApiOperation(value = "旧件返还申请")
//    public ResponseHelper add(
//            @RequestBody OldRefundManageBean oldRefundManageBean
//    ) throws Exception {
//        // 校验
//        ValidateUtil.validate(oldRefundManageBean, ValidGroup.Add.class);
//        for (OldRefundManageItemBean itemBean : oldRefundManageBean.getOldRefundManageItemBeanList()) {
//            try {
//                ValidateUtil.validate(itemBean, ValidGroup.Add.class);
//            } catch (ValidationException e) {
//                throw new RemoteServiceException("明细出错原因：" + e.getMessage());
//            }
//        }
//
//        return ResponseHelper.success(oldRefundManageLogic.add(oldRefundManageBean));
//    }



}
