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

import com.alibaba.fastjson.JSONObject;
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.manage.api.dto.HaobanTaskNoticeDTO;
import com.gic.haoban.manage.api.dto.chat.GroupChatPlanDTO;
import com.gic.haoban.manage.api.dto.moment.QwMomentPlanDTO;
import com.gic.haoban.manage.api.service.HaobanTaskNoticeApiService;
import com.gic.haoban.manage.api.service.chat.GroupChatPlanApiService;
import com.gic.haoban.manage.api.service.moment.QwMomentApiService;
import com.gic.haoban.manage.web.log.LogRecordUserServiceImpl;
import com.gic.log.record.anno.GicLogRecord;
import com.gic.log.record.util.GicLogRecordCategoryEnum;
import com.gic.log.record.util.GicLogRecordEvaluationContext;
import com.gic.log.record.util.GicLogRecordOptTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 任务提醒
 */
@RestController
@RequestMapping("/task-notice")
public class TaskNoticeController {

    private static Logger logger = LogManager.getLogger(TaskNoticeController.class);

    @Autowired
    private HaobanTaskNoticeApiService taskNoticeApiService;
    @Autowired
    private QwMomentApiService qwMomentApiService;
    @Autowired
    private GroupChatPlanApiService groupChatPlanApiService;

    /**
     * 是否需要提醒 3社群 4朋友圈
     * @param planId
     * @param planType
     * @return
     */
    @RequestMapping("has-task")
    @ResponseBody
    public RestResponse<Boolean> hasTask(String planId , int planType){
        return RestResponse.successResult(true)  ;
    }

    /**
     * 朋友圈提醒
     * @param dto
     * @return
     */
    @RequestMapping("moment")
    @ResponseBody
    @GicLogRecord(value = "${#logValue}", category = GicLogRecordCategoryEnum.QW_MOMENT, optType = GicLogRecordOptTypeEnum.QW_MOMENT_TASK_NOTICE, userFunc = LogRecordUserServiceImpl.class, optPage = "企微朋友圈")
    public RestResponse<String> momentSave(@RequestBody HaobanTaskNoticeDTO dto){
        ServiceResponse<Void> resp = this.save(dto,4) ;
        if(!resp.isSuccess()) {
            GicLogRecordEvaluationContext.noWriteLog();
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult()  ;
    }


    /**
     * 客户群提醒
     * @param dto
     * @return
     */
    @RequestMapping("chat")
    @ResponseBody
    @GicLogRecord(value = "${#logValue}", category = GicLogRecordCategoryEnum.HB_QQF, optType = GicLogRecordOptTypeEnum.HB_QQF_TASK_NOTICE, userFunc = LogRecordUserServiceImpl.class, optPage = "客户群群发")
    public RestResponse<String> chatSave(@RequestBody HaobanTaskNoticeDTO dto){
        ServiceResponse<Void> resp = this.save(dto,3) ;
        if(!resp.isSuccess()) {
            GicLogRecordEvaluationContext.noWriteLog();
            return RestResponse.failure("9999",resp.getMessage()) ;
        }
        return RestResponse.successResult()  ;
    }

    private ServiceResponse<Void> save(HaobanTaskNoticeDTO dto , int type) {
        logger.info("新建任务={}", JSONObject.toJSONString(dto));
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        dto.setEnterpriseId(loginUser.getEnterpriseId());
        dto.setCreatorId(loginUser.getClerkId());
        dto.setPlanType(type);
        ServiceResponse<Void> resp = this.taskNoticeApiService.save(dto) ;
        //活动名称-活动id下发任务提醒，提醒说明：这是提醒说明；（无提醒说明时，不显示提醒说明，例：6月新品-245675643456554下发任务提醒）
        String name = "";
        if(type==3) {
            ServiceResponse<GroupChatPlanDTO> detail = this.groupChatPlanApiService.detail(Long.valueOf(dto.getPlanId())) ;
            name = detail.getResult().getName() + "-" + dto.getPlanId() ;
        }
        if(type==4) {
            ServiceResponse<QwMomentPlanDTO> detail = this.qwMomentApiService.detail(Long.valueOf(dto.getPlanId())) ;
            name = detail.getResult().getTitle() + "-" + dto.getPlanId() ;
        }
        name = name +"下发任务提醒" ;
        if(StringUtils.isNotBlank(dto.getRemark())) {
            name = name + "，提醒说明：" + dto.getRemark()  ;
        }
        GicLogRecordEvaluationContext.putAttribute("logValue", name);
        return resp ;
    }

}
