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

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.admin.AdminUserCom;
import com.gree.mall.manager.bean.settle.MonthGatherBean;
import com.gree.mall.manager.bean.settle.SettleMonthWagesVO;
import com.gree.mall.manager.bean.settle.SettleMonthWagesWorkerVO;
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.settle.SettleMonthWagesLogic;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
@Api(value = "月度工资汇总", tags = {"月度工资汇总"})
@RequestMapping(value = "/settle/month/wages", produces = "application/json; charset=utf-8")
public class SettleMonthWagesController {

    @Resource
    private SettleMonthWagesLogic settleMonthWagesLogic;
    @Resource
    private RedisLockRegistry redisLockRegistry;
    @Resource
    private CommonLogic commonLogic;

    @ZfireList
    @PostMapping("/gather/list")
    @ApiOperation(value = "月度工资汇总列表")
    public ResponseHelper<IPage<SettleMonthWagesVO>> gatherList(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<SettleMonthWagesVO> page = settleMonthWagesLogic.gatherList(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<SettleMonthWagesVO>() {
        });
    }

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "月度工资师傅列表")
    public ResponseHelper<IPage<SettleMonthWagesWorkerVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<SettleMonthWagesWorkerVO> page = settleMonthWagesLogic.list(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<SettleMonthWagesWorkerVO>() {
        });
    }

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

    @PostMapping("/batch/reject")
    @ApiOperation(value = "汇总批量驳回")
    public ResponseHelper batchReject(
            @RequestBody List<String> ids
    ) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + ":"
                + (Objects.nonNull(adminUser.getAdminCompanyWechat()) ? adminUser.getAdminCompanyWechat().getCompanyWechatId() : ""));
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再尝试");
        }
        try {
            settleMonthWagesLogic.batchReject(adminUser, ids);
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }

    @PostMapping("/batch/send")
    @ApiOperation(value = "汇总批量发放")
    public ResponseHelper batchSend(
            @RequestBody MonthGatherBean bean
    ) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + ":"
                + (Objects.nonNull(adminUser.getAdminCompanyWechat()) ? adminUser.getAdminCompanyWechat().getCompanyWechatId() : ""));
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再尝试");
        }
        try {
            settleMonthWagesLogic.batchSend(adminUser, bean);
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }

    @PostMapping("/worker/batch/send")
    @ApiOperation(value = "师傅批量发放")
    public ResponseHelper workerBatchSend(
            @RequestBody MonthGatherBean bean
    ) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_ORDER + ":"
                + (Objects.nonNull(adminUser.getAdminCompanyWechat()) ? adminUser.getAdminCompanyWechat().getCompanyWechatId() : ""));
        if (!obtain.tryLock(10, TimeUnit.SECONDS)) {
            throw new RemoteServiceException("系统繁忙，请稍后再尝试");
        }
        try {
            settleMonthWagesLogic.workerBatchSend(adminUser, bean);
        } finally {
            obtain.unlock();
        }

        return ResponseHelper.success();
    }

}
