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

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.workorder.PgAppraiseApplyBean;
import com.gree.mall.manager.bean.workorder.PgAppraiseApplyCountBean;
import com.gree.mall.manager.bean.workorder.PgAppraiseApplyVO;
import com.gree.mall.manager.bean.workorder.PgAppraiseCategoryVO;
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.common.CommonLogic;
import com.gree.mall.manager.logic.workorder.AppraiseRelaLogic;
import com.gree.mall.manager.plus.entity.PgAppraiseCategory;
import com.gree.mall.manager.plus.entity.PgAppraiseConfig;
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 org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "评价申诉API", tags = {"评价申诉API"})
@RequestMapping(value = "/appraise/apply", produces = "application/json; charset=utf-8")
public class AppraiseApplyController {

    @Resource
    AppraiseRelaLogic appraiseRelaLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;
    @Resource
    CommonLogic commonLogic;

    @PostMapping("/config/list")
    @ApiOperation(value = "评价配置-列表")
    public ResponseHelper<List<PgAppraiseConfig>> configList() {
        List<PgAppraiseConfig> config = appraiseRelaLogic.configList();
        return ResponseHelper.success(config);
    }

    @PostMapping("/config/save")
    @ApiOperation(value = "评价配置-保存")
    public ResponseHelper configSave(@RequestBody List<PgAppraiseConfig> config) {
        appraiseRelaLogic.configSave(config);
        return ResponseHelper.success();
    }

    @ZfireList
    @PostMapping("/category/list")
    @ApiOperation(value = "评价问题类型-列表")
    public ResponseHelper<Page<PgAppraiseCategoryVO>> categoryList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<PgAppraiseCategoryVO> vo = appraiseRelaLogic.categoryList(zfireParamBean);
        return ResponseHelper.success(vo, new TypeReference<PgAppraiseCategoryVO>() {});
    }

    @PostMapping("/category/detail")
    @ApiOperation(value = "评价问题类型-详情")
    public ResponseHelper<PgAppraiseCategory> categoryDetail(String id) {
        PgAppraiseCategory detail = appraiseRelaLogic.categoryDetail(id);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/category/save")
    @ApiOperation(value = "评价问题类型-保存")
    public ResponseHelper categorySave(@RequestBody PgAppraiseCategory category) {
        appraiseRelaLogic.categorySave(category);
        return ResponseHelper.success();
    }

    @PostMapping("/category/del")
    @ApiOperation(value = "评价问题类型-大类删除")
    public ResponseHelper categoryDel(@RequestBody List<String> ids) {
        appraiseRelaLogic.categoryDel(ids);
        return ResponseHelper.success();
    }

    @PostMapping("/category/import")
    @ApiOperation(value = "评价问题类型-导入(下载模板:评价问题类别模板.xlsx)")
    public ResponseHelper<String> categoryImportData(
            @RequestPart("file") MultipartFile file
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER);
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("正在导入中，请稍后再试");
        }
        try {
            appraiseRelaLogic.categoryImportData(file);
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }

    @ZfireList
    @PostMapping("/apply/list")
    @ApiOperation(value = "评价申诉-列表")
    public ResponseHelper<Page<PgAppraiseApplyVO>> applyList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<PgAppraiseApplyVO> vo = appraiseRelaLogic.applyList(zfireParamBean);
        return ResponseHelper.success(vo, new TypeReference<PgAppraiseApplyVO>() {});
    }

    @PostMapping("/apply/list/export")
    @ApiOperation(value = "评价申诉-导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //2.查询要导出的内容
        IPage<PgAppraiseApplyVO> page = appraiseRelaLogic.applyList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/apply/count")
    @ApiOperation(value = "评价申诉-统计")
    public ResponseHelper<PgAppraiseApplyCountBean> applyCount() {
        PgAppraiseApplyCountBean count = appraiseRelaLogic.applyCount();
        return ResponseHelper.success(count);
    }

    @PostMapping("/apply/detail")
    @ApiOperation(value = "评价申诉-详情")
    public ResponseHelper<PgAppraiseApplyBean> applyDetail(String id) {
        PgAppraiseApplyBean detail = appraiseRelaLogic.applyDetail(id);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/apply/submit")
    @ApiOperation(value = "评价申诉-提交")
    public ResponseHelper applySubmit(@RequestBody PgAppraiseApplyBean bean) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + adminUser.getAdminUserId());
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("处理中，请稍后再试");
        }
        try {
            appraiseRelaLogic.applySubmit(bean);
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/apply/confirm")
    @ApiOperation(value = "评价申诉-审核")
    public ResponseHelper applyConfirm(@RequestBody PgAppraiseApplyBean bean) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + bean.getId());
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("处理中，请稍后再试");
        }
        try {
            appraiseRelaLogic.applyConfirm(bean);
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }

    @PostMapping("/apply/reject")
    @ApiOperation(value = "评价申诉-驳回待申诉")
    public ResponseHelper applyReject(
            @ApiParam(value = "id", required = true) @RequestParam String id
    ) {
        appraiseRelaLogic.applyReject(id);
        return ResponseHelper.success();
    }

    @PostMapping("/batch/update/reset")
    @ApiOperation(value = "评价申诉-驳回可重申诉")
    public ResponseHelper batchUpdateReset(
            @RequestBody List<String> ids
    ) throws Exception {
        appraiseRelaLogic.batchUpdateReset(ids);
        return ResponseHelper.success();
    }

    @PostMapping("/batch/update/end")
    @ApiOperation(value = "评价申诉-驳回不可再申诉")
    public ResponseHelper batchUpdateEnd(
            @RequestBody List<String> ids
    ) throws Exception {
        appraiseRelaLogic.batchUpdateEnd(ids);
        return ResponseHelper.success();
    }
}
