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

import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.web.qo.PageQo;
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.HaobanResponse;
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.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.notify.PairValVO;
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 {

    @Autowired
    private PendingTaskApiService pendingTaskApiService;

    @Autowired
    private NoticeMessageApiService noticeMessageService;

    /**
     * 待办列表
     *
     * @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());
    }


}
