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

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.quality.QualityFeedbackBean;
import com.gree.mall.manager.bean.quality.QualityFeedbackCountBean;
import com.gree.mall.manager.bean.quality.QualityFeedbackVO;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.enums.quality.ExamineQualityFeedbackStatusEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.quality.QualityFeedbackLogic;
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.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "质量反馈API", tags = {"质量反馈API"})
@RequestMapping(value = "/quality/feedback", produces = "application/json; charset=utf-8")
public class QualityFeedbackController {

    @Resource
    QualityFeedbackLogic qualityFeedbackLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;

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

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

    @PostMapping("/detail")
    @ApiOperation("详情")
    public ResponseHelper<QualityFeedbackBean> detail(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) throws RemoteServiceException {
        QualityFeedbackBean detail = qualityFeedbackLogic.detail(id);
        return ResponseHelper.success(detail);
    }

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

    @PostMapping("/add")
    @ApiOperation("新增")
    public ResponseHelper add(@RequestBody QualityFeedbackBean bean) throws RemoteServiceException {
        qualityFeedbackLogic.add(bean);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseHelper update(@RequestBody QualityFeedbackBean bean) throws RemoteServiceException {
        qualityFeedbackLogic.update(bean);
        return ResponseHelper.success();
    }

    @PostMapping("/update/upload")
    @ApiOperation("已录入总部状态")
    public ResponseHelper manualConfirm(
            @ApiParam(value = "id", required = true) @RequestParam List<String> ids
    ) throws Exception {
        qualityFeedbackLogic.updateUpload(ids);
        return ResponseHelper.success();
    }

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


    /**
     * 压缩远程服务器文件
     */
    @GetMapping("/down/zip")
    @ApiOperation("下载附件")
    public void downloadZip(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            HttpServletResponse response
    )  throws Exception {
        qualityFeedbackLogic.downZipFile(id, response);
    }
}
