package com.willalp.msg.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.utils.StringUtils;
import com.willalp.msg.domain.HsAppUserMsg;
import com.willalp.msg.mapper.HsAppUserMsgMapper;
import com.willalp.msg.service.IHsAppUserMsgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * APP用户消息Service业务层处理
 *
 * @author willalp
 * @date 2022-10-26
 */
@Service
public class HsAppUserMsgServiceImpl extends ServiceImpl<HsAppUserMsgMapper, HsAppUserMsg> implements IHsAppUserMsgService {
    @Resource
    private HsAppUserMsgMapper hsAppUserMsgMapper;

    /**
     * 查询APP用户消息列表
     *
     * @param hsAppUserMsg APP用户消息
     * @return APP用户消息
     */
    @Override
    public List<HsAppUserMsg> selectHsAppUserMsgList(HsAppUserMsg hsAppUserMsg) {
        return hsAppUserMsgMapper.selectHsAppUserMsgList(hsAppUserMsg);
    }

    @Override
    public Object msgSum(String uc) {
        if (StringUtils.isNotBlank(uc)) {
            List<HsAppUserMsg> msgsByUserCode = hsAppUserMsgMapper.selectList(new QueryWrapper<HsAppUserMsg>()
                    .eq("user_code", uc)
                    .eq("msg_is_read", "0"));

            hsAppUserMsgMapper.insertBatchSomeColumn(msgsByUserCode);
            List<HsAppUserMsg> l1 = new ArrayList<>();
            List<HsAppUserMsg> l3 = new ArrayList<>();
            List<HsAppUserMsg> l2 = new ArrayList<>();
            List<HsAppUserMsg> l4 = new ArrayList<>();
            msgsByUserCode.forEach((i) -> {
                switch (i.getMsgType()) {
                    case "1":
                        l1.add(i);
                        break;
                    case "2":
                        l2.add(i);
                        break;
                    case "3":
                        l3.add(i);
                        break;
                    case "4":
                        l4.add(i);
                        break;
                }
            });
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("sum", msgsByUserCode.size());
            jsonObject.put("t1", l1.size());
            jsonObject.put("t2", l2.size());
            jsonObject.put("t3", l3.size());
            jsonObject.put("t4", l4.size());
            return jsonObject;
        }


        return null;
    }

    @Override
    public void readMsgByTaskId(String taskId,String userCode) {
        hsAppUserMsgMapper.readMsgByTaskId(taskId,userCode);
    }

    @Override
    public void readMsgById(String id) {
        if (StringUtils.isNotBlank(id)) {
            hsAppUserMsgMapper.readMsgById(id);
        }
    }

    @Override
    public void allReadMsgByUserCode(String uc) {
        if (StringUtils.isNotBlank(uc)) {
            hsAppUserMsgMapper.allReadMsgByUserCode(uc);
        }
    }
}
