package com.hai.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hai.common.utils.DateUtil;
import com.hai.dao.BsMsgMapper;
import com.hai.dao.BsMsgUserMapper;
import com.hai.entity.*;
import com.hai.service.BsMsgService;
import com.hai.service.HighOrderService;
import com.hai.service.HighUserService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.beans.Transient;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;

/**
 * @author sum1dream
 */
@Service("bsMsgService")
public class BsMsgServiceImpl implements BsMsgService {

    @Resource
    private BsMsgMapper bsMsgMapper;

    @Resource
    private BsMsgUserMapper bsMsgUserMapper;

    @Resource
    private HighOrderService highOrderService;

    @Resource
    private HighUserService highUserService;

    @Override
    @Transactional
    public void insertMsg(BsMsg bsMsg , HighUser user) {

        bsMsg.setCreateTime(new Date());
        bsMsg.setUpdateTime(new Date());

        bsMsgMapper.insert(bsMsg);
        if (bsMsg.getMsgType()==1){
            BsMsgUser bsMsgUser = new BsMsgUser();
            bsMsgUser.setCreateTime(new Date());
            bsMsgUser.setUpdateTime(new Date());
            bsMsgUser.setMsgId(bsMsg.getId());
            bsMsgUser.setUserName(user.getName());
            bsMsgUser.setUserId(user.getId());
            bsMsgUser.setUserPhone(user.getPhone());
            bsMsgUser.setWhereCheck(false);
            bsMsgUserMapper.insert(bsMsgUser);
        }
    }

    @Override
    public void updateMsg(BsMsg bsMsg) {
        bsMsgMapper.updateByPrimaryKey(bsMsg);
    }

    @Override
    public BsMsg findMsg(Long id) {
        return bsMsgMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<BsMsg> getMsgByList(Map<String, Object> map) {

        BsMsgExample example = new BsMsgExample();
        BsMsgExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getInteger(map , "type") != null) {
            criteria.andTypeEqualTo(MapUtils.getInteger(map , "type"));
        }
        if (MapUtils.getInteger(map , "jumpType") != null) {
            criteria.andJumpTypeEqualTo(MapUtils.getInteger(map , "jumpType"));
        }
        if (MapUtils.getInteger(map , "msgType") != null) {
            criteria.andMsgTypeEqualTo(MapUtils.getInteger(map , "msgType"));
        }
        if (MapUtils.getInteger(map , "title") != null) {
            criteria.andTitleLike("%" + MapUtils.getString(map , "title") + "%" );
        }
        if (MapUtils.getLong(map , "companyId") != null) {
            criteria.andCompanyIdEqualTo(MapUtils.getLong(map , "companyId"));
        }
        if (MapUtils.getInteger(map , "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(map , "status"));
        } else {
            criteria.andStatusNotEqualTo(0);
        }

        return bsMsgMapper.selectByExample(example);
    }

    @Override
    public void checkMsg(HighUser user, Long msgId) {
        // 查询用户相关站内信内容
        List<BsMsgUser> list = findMsgUserById(user.getId() , msgId);
        BsMsgUser bsMsgUser = new BsMsgUser();
        // 判读是否有数据
        if (list.size()>0) {
            bsMsgUser = list.get(0);
            // 判断当前内容是否已经查看过了
            if (!bsMsgUser.getWhereCheck()) {
                bsMsgUser.setWhereCheck(true);
                bsMsgUser.setCheckTime(new Date());
            }
            bsMsgUser.setUpdateTime(new Date());
            bsMsgUserMapper.updateByPrimaryKey(bsMsgUser);
        } else {
            bsMsgUser.setUserId(user.getId());
            bsMsgUser.setMsgId(msgId);
            bsMsgUser.setUserName(user.getName());
            bsMsgUser.setUserPhone(user.getPhone());
            bsMsgUser.setWhereCheck(true);
            bsMsgUser.setCheckTime(new Date());
            bsMsgUser.setCreateTime(new Date());
            bsMsgUser.setUpdateTime(new Date());
            bsMsgUserMapper.insert(bsMsgUser);
        }

    }

    @Override
    public List<BsMsgUser> findMsgUserById(Long userId, Long msgId) {
        BsMsgUserExample example = new BsMsgUserExample();
        BsMsgUserExample.Criteria criteria = example.createCriteria();

        criteria.andUserIdEqualTo(userId).andMsgIdEqualTo(msgId);

        return bsMsgUserMapper.selectByExample(example);
    }

    @Override
    public List<BsMsg> queryMsgByList(Map<String, Object> map) {
        return bsMsgUserMapper.queryMsgByList(map);
    }

    @Override
    public void pushUserMsg(Long orderId,String title , Integer type) throws Exception {
        // 查询订单
        HighOrder highOrder = highOrderService.getOrderById(orderId);
        // 查询用户信息
        HighUser highUser = highUserService.findByUserId(highOrder.getMemId());

        JSONObject object = new JSONObject();
        object.put("price" , highOrder.getPayRealPrice());
        object.put("productType" , highOrder.getProductType());
        object.put("objectName" , highOrder.getTitle());
        object.put("orderNo" , highOrder.getOrderNo());
        object.put("time" , DateUtil.date2String(highOrder.getCreateTime() , DateUtil.Y_M_D_HMS_C));

        BsMsg bsMsg = new BsMsg();

        bsMsg.setCompanyId(highOrder.getCompanyId());
        bsMsg.setOpName("系统生成");
        bsMsg.setMsgType(1);
        bsMsg.setType(type);
        bsMsg.setObjectId(orderId);
        bsMsg.setObjectType(highOrder.getPayType());
        bsMsg.setJumpType(1);
        bsMsg.setTitle(title);
        bsMsg.setStatus(2);
        bsMsg.setContent(object.toString());
        insertMsg(bsMsg , highUser);
    }
}
