//package com.gree.mall.manager.controller.sop;
//
//
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.gree.mall.manager.bean.sop.SopPushTaskData;
//import com.gree.mall.manager.bean.sop.SopRuleAdd;
//import com.gree.mall.manager.bean.sop.SopRuleData;
//import com.gree.mall.manager.bean.sop.SopWorkerPushCount;
//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.sop.SopRuleLogic;
//import com.gree.mall.manager.plus.entity.AdminCompanyWechat;
//import com.gree.mall.manager.plus.entity.SopPushTask;
//import com.gree.mall.manager.plus.entity.SopWorkerPush;
//import com.gree.mall.manager.plus.entity.Tag;
//import com.gree.mall.manager.plus.service.AdminCompanyWechatService;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import io.swagger.annotations.ApiParam;
//import io.swagger.models.auth.In;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.integration.redis.util.RedisLockRegistry;
//import org.springframework.web.bind.annotation.*;
//
//import javax.servlet.http.HttpServletRequest;
//import java.io.IOException;
//import java.io.InputStream;
//import java.util.List;
//import java.util.concurrent.TimeUnit;
//import java.util.concurrent.locks.Lock;
//
///**
// * <p>
// * 个人sop 前端控制器
// * </p>
// *
// * @author change
// * @since 2022-04-25
// */
//@Slf4j
//@RestController
//@Api(value = "个人sop", tags = {"个人sop"})
//@RequestMapping(value = "/sopRule", produces = "application/json; charset=utf-8")
//public class SopRuleController {
//
//
//    @Autowired
//    private SopRuleLogic sopRuleLogic;
//
//    @Autowired
//    RedisLockRegistry redisLockRegistry;
//
//    @Autowired
//    AdminCompanyWechatService adminCompanyWechatService;
//
//    @ApiOperation("个人sop-list")
//    @GetMapping("/list")
//    public ResponseHelper<IPage<SopRuleData>> list(
//            HttpServletRequest request,
//            @ApiParam(value = "规则名称", required = false) @RequestParam(required = false) String ruleName,
//            @ApiParam(value = "状态 1启用 0停用", required = false) @RequestParam(required = false) Integer status,
//            @ApiParam(value = "页号", required = true) @RequestParam(required = true) Integer pageNum,
//            @ApiParam(value = "页大小", required = true) @RequestParam(required = true) Integer pageSize
//    ) throws IOException {
//
//        IPage<SopRuleData> list = sopRuleLogic.list(ruleName, status, request, pageNum, pageSize);
//        return ResponseHelper.success(list);
//    }
//
//    @ApiOperation("个人sop-详情")
//    @GetMapping("/detail")
//    public ResponseHelper<SopRuleData> detail(
//            @ApiParam(value = "id", required = true) @RequestParam(required = true) String id
//    ) throws IOException {
//
//        SopRuleData sopRuleData = sopRuleLogic.detail(id);
//        return ResponseHelper.success(sopRuleData);
//    }
//
//
//    @PostMapping("/add")
//    @ApiOperation("个人sop-新增规则")
//    public ResponseHelper add(
//            @ApiParam(value = "客服ID，任务，多个", required = true) @RequestBody SopRuleAdd sopRuleAdd,
//            HttpServletRequest request
//    ) throws RemoteServiceException, InterruptedException {
//
//        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_SOP_SYNC);
//        try {
//            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
//                throw new RemoteServiceException("系统繁忙，请稍后再试");
//            }
//            sopRuleLogic.add(sopRuleAdd, request);
//        } finally {
//            obtain.unlock();
//        }
//        return ResponseHelper.success();
//    }
//
//    @PostMapping("/addKeFu")
//    @ApiOperation("个人sop-新增客服")
//    public ResponseHelper addKeFu(
//            @ApiParam(value = "客服ID，任务，多个", required = true) @RequestBody SopRuleAdd sopRuleAdd,
//            HttpServletRequest request
//    ) throws RemoteServiceException, InterruptedException {
//
//        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_SOP_SYNC);
//        try {
//            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
//                throw new RemoteServiceException("系统繁忙，请稍后再试");
//            }
//            sopRuleLogic.addKeFu(sopRuleAdd, request);
//        } finally {
//            obtain.unlock();
//        }
//        return ResponseHelper.success();
//    }
//
//    @PostMapping("/edit")
//    @ApiOperation("个人sop-修改规则")
//    public ResponseHelper edit(
//            @ApiParam(value = "客服ID，任务，多个", required = true) @RequestBody SopRuleAdd sopRuleAdd,
//            HttpServletRequest request
//    ) throws RemoteServiceException, InterruptedException {
//
//        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_SOP_SYNC);
//        try {
//            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
//                throw new RemoteServiceException("系统繁忙，请稍后再试");
//            }
//            sopRuleLogic.edit(sopRuleAdd, request);
//        } finally {
//            obtain.unlock();
//        }
//        return ResponseHelper.success();
//    }
//
//    @PostMapping("/delete")
//    @ApiOperation("个人sop-删除规则")
//    public ResponseHelper delete(
//            @ApiParam(value = "id", required = true) @RequestParam(required = true) String id
//    ) throws RemoteServiceException, InterruptedException {
//        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_SOP_SYNC+id);
//        try {
//            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
//                throw new RemoteServiceException("系统繁忙，请稍后再试");
//            }
//            sopRuleLogic.delete(id);
//        } finally {
//            obtain.unlock();
//        }
//        return ResponseHelper.success();
//    }
//
//    @PostMapping("/addPushTask")
//    @ApiOperation("个人sop-新增任务")
//    public ResponseHelper addPushTask(
//            @ApiParam(value = "规则ID", required = true) @RequestParam(required = true) String id,
//            @RequestBody SopPushTaskData sopPushTaskData,
//            HttpServletRequest request
//    ) throws RemoteServiceException, InterruptedException {
//        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_SOP_SYNC);
//        try {
//            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
//                throw new RemoteServiceException("系统繁忙，请稍后再试");
//            }
//            sopRuleLogic.addPushTask(sopPushTaskData,id,request);
//        } finally {
//            obtain.unlock();
//        }
//        return ResponseHelper.success();
//    }
//
//    @PostMapping("/editPushTask")
//    @ApiOperation("个人sop-修改任务")
//    public ResponseHelper editPushTask(
//            @RequestBody SopPushTaskData sopPushTaskData,
//            HttpServletRequest request
//    ) throws RemoteServiceException, InterruptedException {
//        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_SOP_SYNC);
//        try {
//            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
//                throw new RemoteServiceException("系统繁忙，请稍后再试");
//            }
//            sopRuleLogic.editPushTask(sopPushTaskData);
//        } finally {
//            obtain.unlock();
//        }
//        return ResponseHelper.success();
//    }
//
//    @PostMapping("/deleteTask")
//    @ApiOperation("个人sop-删除任务")
//    public ResponseHelper deleteTask(
//            @ApiParam(value = "id", required = true) @RequestParam(required = true) String id
//    ) throws RemoteServiceException, InterruptedException {
//        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_SOP_SYNC);
//        try {
//            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
//                throw new RemoteServiceException("系统繁忙，请稍后再试");
//            }
//            sopRuleLogic.deleteTask(id);
//        } finally {
//            obtain.unlock();
//        }
//        return ResponseHelper.success();
//    }
//
//    @ApiOperation("个人sop-任务详情")
//    @GetMapping("/detailTask")
//    public ResponseHelper<SopPushTaskData> detailTask(
//            @ApiParam(value = "id", required = true) @RequestParam(required = true) String id
//    ) throws IOException {
//        SopPushTaskData sopPushTaskData = sopRuleLogic.detailTask(id);
//        return ResponseHelper.success(sopPushTaskData);
//    }
//
//    @ApiOperation("个人sop-客户列表")
//    @GetMapping("/taskList")
//    public ResponseHelper<IPage<SopWorkerPush>> taskList(
//            @ApiParam(value = "任务ID", required = true) @RequestParam(required = true) String taskId,
//            @ApiParam(value = "规则ID", required = true) @RequestParam(required = true) String ruleId,
//            @ApiParam(value = "客户信息", required = false) @RequestParam(required = false) String externalName,
//            @ApiParam(value = "推送开始时间", required = false) @RequestParam(required = false) String startTime,
//            @ApiParam(value = "推送结束时间", required = false) @RequestParam(required = false) String endTime,
//            @ApiParam(value = "员工执行状态 1执行 0未执行", required = false) @RequestParam(required = false)  Integer workerStatus,
//            @ApiParam(value = "页号", required = true) @RequestParam(required = true) Integer pageNum,
//            @ApiParam(value = "页大小", required = true) @RequestParam(required = true) Integer pageSize,
//            HttpServletRequest request
//    ) throws IOException {
//        IPage<SopWorkerPush> sopWorkerPush = sopRuleLogic.taskList(taskId,ruleId,externalName,startTime,endTime,workerStatus,request,pageNum,pageSize);
//        return ResponseHelper.success(sopWorkerPush);
//    }
//
//    @ApiOperation("个人sop-任务统计")
//    @GetMapping("/taskCount")
//    public ResponseHelper<SopWorkerPushCount> taskCount(
//            @ApiParam(value = "任务ID", required = true) @RequestParam(required = true) String taskId,
//            @ApiParam(value = "规则ID", required = true) @RequestParam(required = true) String ruleId,
//            HttpServletRequest request
//    ) throws IOException {
//        SopWorkerPushCount sopWorkerPushCount = sopRuleLogic.taskCount(taskId,ruleId,request);
//        return ResponseHelper.success(sopWorkerPushCount);
//    }
//
//
//    @PostMapping("/addCompanyToken")
//    @ApiOperation("企业配置token-变更")
//    public ResponseHelper addCompanyToken(
//            @ApiParam(value = "token微信事件回调", required = true) @RequestParam(required = true) String token,
//            @ApiParam(value = "encodingAES_key微信事件回调", required = true) @RequestParam(required = true) String encodingAES_key,
//            HttpServletRequest request
//    ) throws RemoteServiceException, InterruptedException {
//        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.LOCK_SOP_SYNC+token);
//        try {
//            if (!obtain.tryLock(5, TimeUnit.SECONDS)) {
//                throw new RemoteServiceException("系统繁忙，请稍后再试");
//            }
//            sopRuleLogic.addCompanyToken(token,encodingAES_key,request);
//        } finally {
//            obtain.unlock();
//        }
//        return ResponseHelper.success();
//    }
//
//    @ApiOperation("企业配置的token-详情")
//    @GetMapping("/detailCompany")
//    public ResponseHelper<SopPushTaskData> detailCompany(
//            HttpServletRequest request
//    ) throws IOException {
//        AdminCompanyWechat adminCompanyWechat = sopRuleLogic.detailCompany(request);
//        return ResponseHelper.success(adminCompanyWechat);
//    }
//}
//
