package com.gic.haoban.manage.web.controller.open;

import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.common.utils.AuthWebRequestUtil;
import com.gic.haoban.common.utils.EntityUtil;
import com.gic.haoban.manage.web.qo.open.AddTaskPageQO;
import com.gic.haoban.manage.web.utils.CreatorUtils;
import com.gic.haoban.task.manage.api.dto.open.OpenQwAddTaskDTO;
import com.gic.haoban.task.manage.api.dto.open.OpenQwAddTaskSettingDTO;
import com.gic.haoban.task.manage.api.dto.open.OpenQwAutoSendDTO;
import com.gic.haoban.task.manage.api.dto.open.OpenQwDelTaskDTO;
import com.gic.haoban.task.manage.api.qdto.open.QwAddTaskPageQDTO;
import com.gic.haoban.task.manage.api.service.OpenQwApiService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 企微托管任务
 */
@RestController
@RequestMapping("/qw-open-task")
public class QywxOpenTaskController {

    @Autowired
    private OpenQwApiService openQwApiService ;

    /**
     * 加好友任务列表
     */
    @RequestMapping("page-add-task")
    public RestResponse<Page<OpenQwAddTaskDTO>> pageAdTask(AddTaskPageQO qo) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = loginUser.getWxEnterpriseId() ;
        String enterpriseId = loginUser.getEnterpriseId() ;
        BasePageInfo basePageInfo = new BasePageInfo() ;
        basePageInfo.setPageNum(qo.getPageNum());
        basePageInfo.setPageSize(qo.getPageSize());
        QwAddTaskPageQDTO qdto = EntityUtil.changeEntityByJSON(QwAddTaskPageQDTO.class,qo) ;
        qdto.setWxEnterpriseId(wxEnterpriseId);
        qdto.setEnterpriseId(enterpriseId);
        // 创建人
        List<String> creatorIdList = CreatorUtils.getCreatorIdList(0);
        if (CollectionUtils.isNotEmpty(creatorIdList)) {
            qdto.setCreatorIdList(creatorIdList);
        }
        ServiceResponse<Page<OpenQwAddTaskDTO>> resp = this.openQwApiService.addTaskPage(qdto,basePageInfo) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult(resp.getResult()) ;
    }

    /**
     * 保存更新加好友任务
     * @param dto
     * @return
     */
    @RequestMapping("save-add-task")
    public RestResponse<OpenQwAddTaskDTO> saveAddTask(OpenQwAddTaskDTO dto) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = loginUser.getWxEnterpriseId() ;
        String enterpriseId = loginUser.getEnterpriseId() ;
        dto.setWxEnterpriseId(wxEnterpriseId);
        dto.setEnterpriseId(enterpriseId);
        ServiceResponse<OpenQwAddTaskDTO> resp = this.openQwApiService.saveAddTask(dto) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult(resp.getResult()) ;
    }

    /**
     * 获取加好友任务详情
     * @param addTaskId
     * @return
     */
    @RequestMapping("get-add-task")
    public RestResponse<OpenQwAddTaskDTO> getAddTask(Long addTaskId) {
        ServiceResponse<OpenQwAddTaskDTO> resp = this.openQwApiService.getAddTask(addTaskId) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult(resp.getResult()) ;
    }

    /**
     * 终止加好友任务
     * @param addTaskId
     * @return
     */
    @RequestMapping("stop-add-task")
    public RestResponse<Void> stopAddTask(Long addTaskId) {
        ServiceResponse<Void> resp = this.openQwApiService.updateAddTaskStatus(addTaskId,1) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult() ;
    }


    /**
     * 添加好友规则设置
     * @return
     */
    @RequestMapping("get-add-task-setting")
    public RestResponse<OpenQwAddTaskSettingDTO> getTaskSetting() {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = loginUser.getWxEnterpriseId() ;
        String enterpriseId = loginUser.getEnterpriseId() ;
        ServiceResponse<OpenQwAddTaskSettingDTO> resp = this.openQwApiService.geAddSetting(wxEnterpriseId,enterpriseId) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult(resp.getResult()) ;
    }

    /**
     * 保存添加好友规则配置
     * @param dto
     * @return
     */
    @RequestMapping("save-add-task-setting")
    public RestResponse<OpenQwAddTaskSettingDTO> saveTaskSetting(OpenQwAddTaskSettingDTO dto) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = loginUser.getWxEnterpriseId() ;
        String enterpriseId = loginUser.getEnterpriseId() ;
        dto.setWxEnterpriseId(wxEnterpriseId);
        dto.setEnterpriseId(enterpriseId);
        ServiceResponse<OpenQwAddTaskSettingDTO> resp = this.openQwApiService.saveAddSetting(dto) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult(resp.getResult()) ;
    }


    /**
     * 删除好友配置
     * @return
     */
    @RequestMapping("get-del-task-setting")
    public RestResponse<OpenQwDelTaskDTO> getDelTaskSetting() {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = loginUser.getWxEnterpriseId() ;
        String enterpriseId = loginUser.getEnterpriseId() ;
        ServiceResponse<OpenQwDelTaskDTO> resp = this.openQwApiService.getDelTask(wxEnterpriseId,enterpriseId) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult(resp.getResult()) ;
    }

    /**
     * 保存删除好友配置
     * @param dto
     * @return
     */
    @RequestMapping("save-del-task-setting")
    public RestResponse<OpenQwDelTaskDTO> saveDelTaskSetting(OpenQwDelTaskDTO dto) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = loginUser.getWxEnterpriseId() ;
        String enterpriseId = loginUser.getEnterpriseId() ;
        dto.setWxEnterpriseId(wxEnterpriseId);
        dto.setEnterpriseId(enterpriseId);
        ServiceResponse<OpenQwDelTaskDTO> resp = this.openQwApiService.saveDelTask(dto) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult(resp.getResult()) ;
    }


    /**
     * 任务自动执行配置
     * @return
     */
    @RequestMapping("get-auto-send-task-setting")
    public RestResponse<OpenQwAutoSendDTO> getAutoSendTaskSetting() {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = loginUser.getWxEnterpriseId() ;
        String enterpriseId = loginUser.getEnterpriseId() ;
        ServiceResponse<OpenQwAutoSendDTO> resp = this.openQwApiService.getAutoSend(wxEnterpriseId,enterpriseId) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult(resp.getResult()) ;
    }

    /**
     * 保存任务自动执行配置
     * @param dto
     * @return
     */
    @RequestMapping("save-auto-send-task-setting")
    public RestResponse<OpenQwAutoSendDTO> saveAutoSendTaskSetting(OpenQwAutoSendDTO dto) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String wxEnterpriseId = loginUser.getWxEnterpriseId() ;
        String enterpriseId = loginUser.getEnterpriseId() ;
        dto.setWxEnterpriseId(wxEnterpriseId);
        dto.setEnterpriseId(enterpriseId);
        ServiceResponse<OpenQwAutoSendDTO> resp = this.openQwApiService.saveAutoSend(dto) ;
        if(!resp.isSuccess()) {
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult(resp.getResult()) ;
    }


}
