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

import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.clerk.api.dto.ClerkDTO;
import com.gic.clerk.api.service.ClerkService;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.haoban.base.api.common.PageResult2;
import com.gic.haoban.common.utils.EntityUtil;
import com.gic.haoban.common.utils.PageUtil;
import com.gic.haoban.manage.api.dto.notify.dto.NoticeMessageInfoDTO;
import com.gic.haoban.manage.api.dto.notify.dto.PendingTaskDetailDTO;
import com.gic.haoban.manage.api.dto.notify.qdto.PendingListQDTO;
import com.gic.haoban.manage.api.enums.NoticeMessageCategoryTypeEnum;
import com.gic.haoban.manage.api.enums.PendingTaskTypeEnum;
import com.gic.haoban.manage.api.service.chat.GroupChatPlanApiService;
import com.gic.haoban.manage.api.service.notify.NoticeMessageApiService;
import com.gic.haoban.manage.api.service.notify.PendingTaskApiService;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
import com.gic.haoban.manage.web.qo.CommonQO;
import com.gic.haoban.manage.web.qo.PendingListQO;
import com.gic.haoban.manage.web.qo.notify.MessageListQO;
import com.gic.haoban.manage.web.vo.MarketingCountVO;
import com.gic.haoban.manage.web.vo.notify.PairValVO;
import com.gic.haoban.task.api.service.TelTaskApiService;
import com.gic.haoban.task.manage.api.service.IMarketClueTaskApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created 2021/12/16.
 *
 * @author hua
 */
@RestController
public class NotifyController extends WebBaseController {

    private static final Logger logger = LoggerFactory.getLogger(NotifyController.class);
    @Autowired
    private PendingTaskApiService pendingTaskApiService;

    @Autowired
    private NoticeMessageApiService noticeMessageService;

    @Autowired
    private GroupChatPlanApiService groupChatPlanApiService;

    @Autowired
    private ClerkService clerkService;
    @Autowired
    private IMarketClueTaskApiService iMarketClueTaskApiService;
    @Autowired
    private TelTaskApiService telTaskApiService;

    /**
     * 待办列表
     *
     * @return
     */
    @RequestMapping("/commission/list")
    public RestResponse pendingList(@RequestBody @Valid PendingListQO qo) {
        PendingListQDTO pendingListQDTO = EntityUtil.changeEntityByOrika(PendingListQDTO.class, qo);
        pendingListQDTO.setFinishFlag(0);
        pendingListQDTO.setOverdueFlag(0);
        if (qo.getFilterStatus() == 1) {
            pendingListQDTO.setOverdueFlag(1);
        }
        if (qo.getShowSelf() == 0) {
            pendingListQDTO.setClerkId(null);
        }
        ServiceResponse<Page<PendingTaskDetailDTO>> retPage = pendingTaskApiService.pagePendingTask(pendingListQDTO, qo.getBasePageInfo());

        PageResult2 pageInfo = PageUtil.getPageInfo(retPage.getResult());
        return RestResponse.successResult(pageInfo);
    }

    /**
     * 今日待办列表 最多20条
     *
     * @return
     */
    @RequestMapping("/commission/today-list")
    public RestResponse pendingToday(@RequestBody @Valid CommonQO qo) {
        ServiceResponse<List<PendingTaskDetailDTO>> response = pendingTaskApiService
                .listPendingTaskToday(qo.getEnterpriseId(), qo.getStoreId(), qo.getClerkId(), false);
        return RestResponse.successResult(response.getResult());
    }


    /**
     * 今日待办列表 最多20条
     *
     * @return
     */
    @RequestMapping("/commission/filter-info")
    public RestResponse filterInfo(@RequestBody @Valid CommonQO qo) {
        Map<String, Object> ret = new HashMap<String, Object>();

        List<PairValVO> taskTypes = new ArrayList<PairValVO>();
        List<PairValVO> filter = new ArrayList<PairValVO>();
        PendingTaskTypeEnum[] values = PendingTaskTypeEnum.values();
        for (PendingTaskTypeEnum val : values) {
            PairValVO vo = new PairValVO();
            vo.setKey(String.valueOf(val.getType()));
            vo.setName(val.getName());
            taskTypes.add(vo);
        }
        {
            PairValVO vo = new PairValVO();
            vo.setKey("0");
            vo.setName("未逾期");
            filter.add(vo);
        }
        {
            PairValVO vo = new PairValVO();
            vo.setKey("1");
            vo.setName("已逾期");
            filter.add(vo);
        }
        ret.put("taskTypes", taskTypes);
        ret.put("filter", filter);

        return RestResponse.successResult(ret);
    }


    /**
     * 今日待办列表 最多20条
     *
     * @return
     */
    @RequestMapping("/message/list")
    public RestResponse list(@RequestBody @Valid MessageListQO qo) {
        ServiceResponse<Page<NoticeMessageInfoDTO>> retPage = noticeMessageService.pageNoticeMessage(qo.getEnterpriseId(), qo.getStoreId(), qo.getClerkId(), qo.getCategoryType(), qo.getBasePageInfo());
        return RestResponse.successResult(PageUtil.getPageInfo(retPage.getResult()));
    }

    /**
     * 设置单条已读
     *
     * @return
     */
    @RequestMapping("/message/read")
    public RestResponse readSingle(Long noticeMessageId) {
        if (noticeMessageId == null) {
            return RestResponse.failure(HaoBanErrCode.ERR_2.getCode() + "", HaoBanErrCode.ERR_2.getMsg());
        }
        noticeMessageService.updateReadFlagById(noticeMessageId);
        return RestResponse.successResult();
    }

    /**
     * 批量设置已读
     *
     * @return
     */
    @RequestMapping("/message/all-read")
    public RestResponse readAll(@RequestBody @Valid MessageListQO qo) {
        noticeMessageService.updateWriteFlagByClerkId(qo.getEnterpriseId(), qo.getClerkId(), qo.getCategoryType());
        return RestResponse.successResult();
    }

    /**
     * 消息类别
     *
     * @return
     */
    @RequestMapping("/message/category")
    public RestResponse category(@RequestBody @Valid MessageListQO qo) {
        List<PairValVO> ret = new ArrayList<PairValVO>();
        NoticeMessageCategoryTypeEnum[] values = NoticeMessageCategoryTypeEnum.values();
        for (NoticeMessageCategoryTypeEnum val : values) {
            PairValVO vo = new PairValVO();
            vo.setKey(String.valueOf(val.getType()));
            vo.setName(val.getName());
            ret.add(vo);
        }
        return RestResponse.successResult(ret);
    }


    /**
     * 消息类别
     *
     * @return
     */
    @RequestMapping("/message/count")
    public RestResponse messageCount(@RequestBody @Valid MessageListQO qo) {
        ServiceResponse<Integer> ret = noticeMessageService.countUnReadNoticeMessage(qo.getEnterpriseId(), qo.getStoreId(), qo.getClerkId());
        return RestResponse.successResult(ret.getResult());
    }

    /**
     * 查询营销模块未完成任务数量
     *
     * @param qo
     * @return
     */
    @RequestMapping("/marketing/count")
    public RestResponse<MarketingCountVO> marketingCount(@RequestBody @Valid CommonQO qo) {

        String clerkId = qo.getClerkId();
        String enterpriseId = qo.getEnterpriseId();
        String storeId = qo.getStoreId();
        MarketingCountVO marketingCountVO = new MarketingCountVO();
        logger.info("查询是否店长：{}", clerkId);
        ClerkDTO clerkDTO = clerkService.getClerkByClerkId(clerkId);
        if (clerkDTO != null) {
            Integer groupChatCount, telTaskCount, groupMessageCount, total = 0;
            Integer clerkType = clerkDTO.getClerkType();
            if (clerkType == 1 || clerkType == 2) {
                //店长维度
                //社群群发
                logger.info("店长维度查询社群群发");
                groupChatCount = groupChatPlanApiService.getTotalCount(enterpriseId, null, storeId).getResult();
                //话务群发
                logger.info("店长维度查询话务群发");
                telTaskCount = telTaskApiService.getTotalCount(enterpriseId, null, storeId).getResult();
                //群发
                logger.info("店长维度查询群发");
                groupMessageCount = iMarketClueTaskApiService.getTotalCount(enterpriseId, null, storeId).getResult();
                total = groupChatCount + telTaskCount + groupMessageCount;
            } else {
                //导购维度
                //社群群发
                logger.info("导购维度查询社群群发");
                groupChatCount = groupChatPlanApiService.getTotalCount(enterpriseId, clerkId, storeId).getResult();
                //话务群发
                logger.info("导购维度查询话务群发");
                telTaskCount = telTaskApiService.getTotalCount(enterpriseId, clerkId, storeId).getResult();
                //群发
                logger.info("导购维度查询群发");
                groupMessageCount = iMarketClueTaskApiService.getTotalCount(enterpriseId, clerkId, storeId).getResult();
                total = groupChatCount + telTaskCount + groupMessageCount;
            }
            marketingCountVO.setGroupChatCount(groupChatCount);
            marketingCountVO.setGroupMessageCount(groupMessageCount);
            marketingCountVO.setTelTaskCount(telTaskCount);
            marketingCountVO.setTotal(total);
        }
        return RestResponse.successResult(marketingCountVO);
    }


}
