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

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.contract.SettleRelaConfigVO;
import com.gree.mall.manager.bean.contract.WebsitFollowConfigBean;
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.contract.SettleRelaConfigLogic;
import com.gree.mall.manager.plus.entity.AdminWebsit;
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.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@Slf4j
@RestController
@Api(value = "结算上下级关系API", tags ={"结算上下级关系API"} )
@RequestMapping(value = "/settle/rela/config", produces = "application/json; charset=utf-8")
public class SettleRelaConfigController {

    @Resource
    SettleRelaConfigLogic settleRelaConfigLogic;
    @Resource
    RedisLockRegistry redisLockRegistry;


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

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

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

    @PostMapping("/query/sub/websit")
    @ApiOperation(value = "查询下级网点")
    public ResponseHelper<IPage<AdminWebsit>> querySubWebsit(
            @ApiParam(value = "网点编号") @RequestParam(required = false) String websitId,
            @ApiParam(value = "网点名称") @RequestParam(required = false) String websitName,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws RemoteServiceException {
        IPage<AdminWebsit> list = settleRelaConfigLogic.querySubWebsit(websitId, websitName, pageNum, pageSize);
        return ResponseHelper.success(list);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加")
    public ResponseHelper add(
            @RequestBody WebsitFollowConfigBean bean
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + bean.getWebsitId());
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                settleRelaConfigLogic.add(bean);
            }
        } catch (Exception e) {
            log.error("【添加上下级关系处理】失败", e);
            throw e;
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "编辑")
    public ResponseHelper edit(
            @RequestBody WebsitFollowConfigBean bean
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + bean.getWebsitId());
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                settleRelaConfigLogic.edit(bean);
            }
        } catch (Exception e) {
            log.error("【编辑上下级关系处理】失败", e);
            throw e;
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }

    @PostMapping("/import")
    @ApiOperation(value = "导入(下载模板:结算上下级关系配置模板.xlsx)")
    public ResponseHelper<String> materialImportData (
            @RequestPart("file") MultipartFile file
    ) throws Exception {
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + ":settlerelaimport");
        try {
            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
                settleRelaConfigLogic.importData(file);
            }
        } catch (Exception e) {
            log.error("【导入上下级关系处理】失败", e);
            throw e;
        } finally {
            obtain.unlock();
        }
        return ResponseHelper.success();
    }
}
