package com.fs.crm.controller;

import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.R;
import com.fs.common.utils.ServletUtils;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.crm.domain.CrmMsg;
import com.fs.crm.service.ICrmMsgService;
import com.fs.crm.vo.CrmMsgTypeVO;
import com.fs.system.service.ISysDictDataService;
import com.fs.system.vo.DictVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 *  消息Controller
 * 
 * @author fs
 * @date 2021-04-16
 */
@RestController
@RequestMapping("/crm/msg")
public class CrmMsgController extends BaseController
{
    @Autowired
    private ICrmMsgService crmMsgService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISysDictDataService dictDataService;


    @GetMapping("/getMsgCount")
    public R getMsgCount(HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long count= crmMsgService.selectCrmMsgCountByUserId(loginUser.getUser().getUserId());
        return R.ok().put("counts",count);

    }

    @GetMapping("/getMsg")
    public R getMsg(HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        //获取用户未读总数
        //获取所有类型
        List<DictVO> types=dictDataService.selectDictDataListByType("crm_msg_type");
        List<CrmMsgTypeVO> counts=new ArrayList<>();
        for(DictVO v:types){
            Long count= crmMsgService.selectCrmMsgCountByUserId(loginUser.getUser().getUserId(),Integer.parseInt(v.getDictValue()));
            CrmMsgTypeVO typeBO=new CrmMsgTypeVO();
            typeBO.setMsgType(Integer.parseInt(v.getDictValue()));
            typeBO.setTotal(count);
            typeBO.setMsgTypeName(v.getDictLabel());
            counts.add(typeBO);
        }
        return R.ok().put("counts",counts);
    }



    @GetMapping("/getMsgList")
    public R getMsgList(
            HttpServletRequest request,
            @ApiParam(required = true, name = "type", value = "类型") @RequestParam(value = "type", required = false) Integer type
    ){

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        CrmMsg map=new CrmMsg();
        map.setMsgType(type);
        map.setCompanyUserId(loginUser.getUser().getUserId());
        startPage();
        List<CrmMsg> list = crmMsgService.selectCrmMsgList(map);
        PageInfo<CrmMsg> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    @PostMapping("/setRead")
    public R setRead(@RequestBody CrmMsg msg)
    {
        crmMsgService.updateCrmMsg(msg);
        return R.ok();
    }

    @PostMapping("/setAllRead")
    public R setAllRead()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        crmMsgService.setAllRead(loginUser.getUser().getUserId());
        return R.ok();
    }


}
