package com.gree.mall.manager.controller.material.manage;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.material.manage.WebsitPartsChangeSalesVO;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.material.manage.WebsitPartsChangeSalesLogic;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
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.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "新件转销售", tags = {"新件转销售"})
@RequestMapping(value = "/parts/change-sales", produces = "application/json; charset=utf-8")
public class WebsitPartsChangeSalesController {

    @Resource
    WebsitPartsChangeSalesLogic websitPartsChangeSalesLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation("新件转销售列表")
    public ResponseHelper<IPage<WebsitPartsChangeSalesVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<WebsitPartsChangeSalesVO> partsBeanIPage = websitPartsChangeSalesLogic.pageList(zfireParamBean);
        return ResponseHelper.success(partsBeanIPage, new TypeReference<WebsitPartsChangeSalesVO>() {
        });
    }

    @PostMapping("/list/export")
    @ApiOperation("新件转销售导出")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<WebsitPartsChangeSalesVO> stockBeanIPage = websitPartsChangeSalesLogic.pageList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(stockBeanIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "新件转销售详情")
    public ResponseHelper detail(
            @ApiParam(value = "转销单号", required = true) @RequestParam String changeSalesNo
    ) throws RemoteServiceException {
        return ResponseHelper.success(websitPartsChangeSalesLogic.detail(changeSalesNo));
    }

    @PostMapping("/examine")
    @ApiOperation(value = "新件转销售审批")
    public ResponseHelper refund(
            @ApiParam(value = "转销单号", required = true) @RequestParam String changeSalesNo,
            @ApiParam(value = "审核结果 AGREE=通过 REJECT=驳回", required = true) @RequestParam String flag,
            @ApiParam(value = "审批备注") @RequestParam(required = false) String examineRemark
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.NEW_CHANGE + changeSalesNo);
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                websitPartsChangeSalesLogic.examine(changeSalesNo, flag, examineRemark);
            }
        } catch (Exception e) {
            log.error("【新件转销售处理】失败", e);
            throw e;
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }
}
