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

import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.clerk.api.dto.AuthorizedUser;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.util.PageHelperUtils;
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.moment.QwMomentPlanAttendDTO;
import com.gic.haoban.manage.api.dto.moment.QwMomentPlanDTO;
import com.gic.haoban.manage.api.dto.moment.QwMomentPlanDataDTO;
import com.gic.haoban.manage.api.qdto.moment.PlanClerkListQDTO;
import com.gic.haoban.manage.api.qdto.moment.PlanDataListQDTO;
import com.gic.haoban.manage.api.qdto.moment.QwMomentPlanQDTO;
import com.gic.haoban.manage.api.service.moment.QwMomentApiService;
import com.gic.haoban.manage.web.log.LogRecordUserServiceImpl;
import com.gic.haoban.manage.web.qo.moment.PlanClerkListQO;
import com.gic.haoban.manage.web.qo.moment.PlanDataListQO;
import com.gic.haoban.manage.web.qo.moment.PlanListQO;
import com.gic.haoban.manage.web.qo.moment.QwMomentPlanQO;
import com.gic.haoban.manage.web.utils.CreatorUtils;
import com.gic.haoban.manage.web.vo.moment.HaobanQwMomentPlanVO;
import com.gic.haoban.manage.web.vo.moment.QwMomentPlanAttendVO;
import com.gic.haoban.manage.web.vo.moment.QwMomentPlanDataVO;
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 com.gic.web.common.utils.SessionContextUtils;
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("/qw-moment")
public class QwMomentController {

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

    @Autowired
    private QwMomentApiService qwMomentApiService ;

    /**
     * 新建任务
     */
    @RequestMapping("add")
    @ResponseBody
    public RestResponse<String> add(@RequestBody QwMomentPlanQO qo){
        logger.info("新建任务={}", JSONObject.toJSONString(qo));
        QwMomentPlanDTO plan = EntityUtil.changeEntityByJSON(QwMomentPlanDTO.class,qo) ;
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        plan.setEnterpriseId(loginUser.getEnterpriseId());
        plan.setWxEnterpriseId(loginUser.getWxEnterpriseId());
        plan.setCreatorId(loginUser.getClerkId());
        plan.setCreatorName(loginUser.getClerkName());
        ServiceResponse<QwMomentPlanDTO> resp = this.qwMomentApiService.save(plan) ;
        return RestResponse.successResult(resp.getResult().getPlanId()+"")  ;
    }


    /**
     * 更新
     */
    @RequestMapping("update")
    @ResponseBody
    // @GicLogRecord(value = "${#logValue}", category = GicLogRecordCategoryEnum.CLERKTASK, optType = GicLogRecordOptTypeEnum.CLERKTASK_EIDT, userFunc = LogRecordUserServiceImpl.class, optPage = "营销-导购任务")
    public RestResponse<String> update(@RequestBody QwMomentPlanQO qo) {
        logger.info("更新任务={}", JSONObject.toJSONString(qo));
        if(null == qo.getPlanId()) {
            return RestResponse.failure("9999","活动id为空") ;
        }
        QwMomentPlanDTO plan = EntityUtil.changeEntityByJSON(QwMomentPlanDTO.class,qo) ;
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        plan.setEnterpriseId(loginUser.getEnterpriseId());
        plan.setWxEnterpriseId(loginUser.getWxEnterpriseId());
        plan.setCreatorId(loginUser.getClerkId());
        plan.setCreatorName(loginUser.getClerkName());
        ServiceResponse<QwMomentPlanDTO> resp = this.qwMomentApiService.save(plan) ;
        return RestResponse.successResult(resp.getResult().getPlanId()+"")  ;
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping("del")
    @ResponseBody
    //@GicLogRecord(value = "${#logValue}", category = GicLogRecordCategoryEnum.CLERKTASK, optType = GicLogRecordOptTypeEnum.CLERKTASK_DEL, userFunc = LogRecordUserServiceImpl.class, optPage = "营销-导购任务")
    public RestResponse<Void> del(Long planId){
        ServiceResponse<QwMomentPlanDTO> resp = this.qwMomentApiService.detail(planId) ;
        if(null == resp.getResult()) {
            GicLogRecordEvaluationContext.noWriteLog();
            return RestResponse.failure("9999","活动不存在") ;
        }
        this.qwMomentApiService.del(planId) ;
        String logContent = "【" + resp.getResult().getTitle() + "-" + planId + "】活动被删除";
        GicLogRecordEvaluationContext.putAttribute("logValue", logContent);
        return RestResponse.successResult() ;
    }

    /**
     * 终止
     * @return
     */
    @RequestMapping("stop")
    @ResponseBody
    //@GicLogRecord(value = "${#logValue}", category = GicLogRecordCategoryEnum.CLERKTASK, optType = GicLogRecordOptTypeEnum.CLERKTASK_STOP, userFunc = LogRecordUserServiceImpl.class, optPage = "营销-导购任务")
    public RestResponse<Void> stop(Long planId){
        ServiceResponse<QwMomentPlanDTO> resp = this.qwMomentApiService.detail(planId) ;
        if(null == resp.getResult()) {
            GicLogRecordEvaluationContext.noWriteLog();
            return RestResponse.failure("9999","活动不存在") ;
        }
        this.qwMomentApiService.stop(planId) ;
        String logContent = "【" + resp.getResult().getTitle() + "-" + planId + "】活动被终止";
        GicLogRecordEvaluationContext.putAttribute("logValue", logContent);
        return RestResponse.successResult() ;
    }

    /**
     * 复制
     * @return
     */
    @RequestMapping("copy")
    @ResponseBody
    public RestResponse<HaobanQwMomentPlanVO> copy(Long planId){
        RestResponse<HaobanQwMomentPlanVO> detailResponse = this.detail(planId) ;
        // 删除ID
        HaobanQwMomentPlanVO vo = detailResponse.getResult() ;
        vo.setPlanId(null);
        boolean isAdmin = false ;
        AuthorizedUser loginUser = SessionContextUtils.getLoginUser() ;
        if (loginUser.getSuperAdmin() == 1 ||  null== loginUser.getDepartAuth() ||  3 == loginUser.getDepartAuth()) {
            isAdmin = true ;
        }
        vo.setBeginDate(null);
        vo.setEndDate(null);
        vo.setPublishTime(null);
        return RestResponse.successResult(vo) ;
    }


    /**
     * 活动列表
     */
    @RequestMapping("plan-list")
    public RestResponse<Page<HaobanQwMomentPlanVO>> planList(@RequestBody PlanListQO qo) {
        QwMomentPlanQDTO qdto =  EntityUtil.changeEntityByJSON(QwMomentPlanQDTO.class,qo) ;
        qdto.setCreatorIdList(CreatorUtils.getCreatorIdList(qo.getShowSelfFlag()));
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        qdto.setEnterpriseId(loginUser.getEnterpriseId());
        qdto.setWxEnterpriseId(loginUser.getWxEnterpriseId());
        ServiceResponse<Page<QwMomentPlanDTO>> resp = this.qwMomentApiService.planList(qdto) ;
        Page<HaobanQwMomentPlanVO> retPage = PageHelperUtils.changePageToCurrentPage(resp.getResult(), HaobanQwMomentPlanVO.class);
        return RestResponse.successResult(retPage);
    }

    /**
     * 活动列表-导出
     * @param qo
     * @return
     */
    @RequestMapping("plan-list-excel")
    public RestResponse<String> planListExcel(@RequestBody PlanListQO qo) {
        QwMomentPlanQDTO qdto = new QwMomentPlanQDTO() ;
        ServiceResponse<Page<QwMomentPlanDTO>> resp = this.qwMomentApiService.planList(qdto) ;
        return RestResponse.successResult(null);
    }

    /**
     * 活动详情
     */
    @RequestMapping("detail")
    public RestResponse<HaobanQwMomentPlanVO> detail(Long planId) {
        ServiceResponse<QwMomentPlanDTO> detaiResp = this.qwMomentApiService.detail(planId) ;
        if (!detaiResp.isSuccess()) {
            return RestResponse.failure("1", "任务不存在");
        }
        // 查询素材列表
        HaobanQwMomentPlanVO vo = EntityUtil.changeEntityByJSON(HaobanQwMomentPlanVO.class, detaiResp.getResult());
        return RestResponse.successResult(vo);
    }


    /**
     * 执行导购列表
     */
    @RequestMapping("clerk-list")
    public RestResponse<Page<QwMomentPlanAttendVO>> clerkList(@RequestBody PlanClerkListQO qo) {
        PlanClerkListQDTO qdto = EntityUtil.changeEntityByJSON(PlanClerkListQDTO.class,qo) ;
        ServiceResponse<Page<QwMomentPlanAttendDTO>> resp = this.qwMomentApiService.attendList(qdto) ;
        Page<QwMomentPlanAttendVO> retPage = PageHelperUtils.changePageToCurrentPage(resp.getResult(), QwMomentPlanAttendVO.class);
        return RestResponse.successResult(retPage);
    }

    /**
     * 执行导购列表-导出
     * @param qo
     * @return
     */
    @RequestMapping("clerk-list-excel")
    public RestResponse<String> clerkListExcel(@RequestBody PlanClerkListQO qo) {
        PlanClerkListQDTO qdto = EntityUtil.changeEntityByJSON(PlanClerkListQDTO.class,qo) ;
        ServiceResponse<Page<QwMomentPlanAttendDTO>> resp = this.qwMomentApiService.attendList(qdto) ;
        return RestResponse.successResult(null);
    }

    /**
     * 统计
     */
    @RequestMapping("data-list")
    public RestResponse<Page<QwMomentPlanDataVO>> dataList(@RequestBody PlanDataListQO qo) {
        PlanDataListQDTO qdto = EntityUtil.changeEntityByJSON(PlanDataListQDTO.class,qo) ;
        ServiceResponse<Page<QwMomentPlanDataDTO>> resp = this.qwMomentApiService.dataList(qdto) ;
        Page<QwMomentPlanDataVO> retPage = PageHelperUtils.changePageToCurrentPage(resp.getResult(), QwMomentPlanDataVO.class);
        return RestResponse.successResult(retPage);
    }


    /**
     * 统计-导出
     */
    @RequestMapping("data-list-excel")
    public RestResponse<String> dataListExcel(@RequestBody PlanDataListQO qo) {
        PlanDataListQDTO qdto = EntityUtil.changeEntityByJSON(PlanDataListQDTO.class,qo) ;
        ServiceResponse<Page<QwMomentPlanDataDTO>> resp = this.qwMomentApiService.dataList(qdto) ;
        return RestResponse.successResult(null);
    }

}
