package com.gree.mall.manager.controller.worker;

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.worker.WorkerElecConfirmManualVO;
import com.gree.mall.manager.bean.worker.WorkerElecConfirmVO;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.enums.ExamineStatusEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.worker.WorkerElecConfirmLogic;
import com.gree.mall.manager.plus.entity.WorkerSignConfirmManual;
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 = "/worker/elec/confirm", produces = "application/json; charset=utf-8")
public class WorkerElecConfirmController {

    @Resource
    WorkerElecConfirmLogic workerElecConfirmLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "列表")
    public ResponseHelper<IPage<WorkerElecConfirmVO>> list(@RequestBody ZfireParamBean zfireParamBean) {
        IPage<WorkerElecConfirmVO> list = workerElecConfirmLogic.list(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<WorkerElecConfirmVO>() {
        });
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //2.查询要导出的内容
        IPage<WorkerElecConfirmVO> list = workerElecConfirmLogic.list(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/detail")
    @ApiOperation("详情")
    public ResponseHelper<WorkerElecConfirmVO> detail(
            @ApiParam(value = "用户id", required = true) @RequestParam String userId,
            @ApiParam(value = "公司编号", required = true) @RequestParam String belongCompanyCode
    ) throws RemoteServiceException {
        WorkerElecConfirmVO detail = workerElecConfirmLogic.detail(userId, belongCompanyCode);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/count")
    @ApiOperation("统计")
    public ResponseHelper<Integer> count() throws RemoteServiceException {
        Integer count = workerElecConfirmLogic.count();
        return ResponseHelper.success(count);
    }

    @ZfireList
    @PostMapping("/manual/list")
    @ApiOperation(value = "人工列表")
    public ResponseHelper<IPage<WorkerElecConfirmManualVO>> manualList(@RequestBody ZfireParamBean zfireParamBean) {
        IPage<WorkerElecConfirmManualVO> list = workerElecConfirmLogic.manualList(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<WorkerElecConfirmManualVO>() {
        });
    }

    @PostMapping("/manual/detail")
    @ApiOperation("人工详情")
    public ResponseHelper<WorkerSignConfirmManual> manualDetail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws RemoteServiceException {
        WorkerSignConfirmManual detail = workerElecConfirmLogic.manualDetail(id);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/manual/confirm")
    @ApiOperation("人工审核")
    public ResponseHelper manualConfirm(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            @ApiParam(value = "状态 OK=通过 FAIL=驳回", required = true) @RequestParam ExamineStatusEnum status
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + id);
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("记录冻结中");
        }
        try {
            workerElecConfirmLogic.manualConfirm(id, status);
            return ResponseHelper.success();
        } catch (Exception e) {
            log.error("【人工审核处理】失败", e);
            throw e;
        } finally {
            obtain.unlock();
        }
    }

    @PostMapping("/manual/count")
    @ApiOperation("人工统计")
    public ResponseHelper<Integer> manualCount() throws RemoteServiceException {
        Integer count = workerElecConfirmLogic.manualCount();
        return ResponseHelper.success(count);
    }

}
