package cn.exploring.engine.server.db.service;

import cn.exploring.engine.server.db.dao.WxUserActionMapper;
import cn.exploring.engine.server.db.domain.WxUserAction;
import cn.exploring.engine.server.db.domain.WxUserActionExample;
import cn.exploring.engine.server.db.util.DateTimeUtil;
import cn.exploring.engine.server.db.util.ResponseUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

@Service
public class WxUserActionService {
    @Resource
    private WxUserActionMapper userActionMapper;

    //根据条件查询列表
    public Object list(Integer userId) {
        WxUserActionExample example = new WxUserActionExample();
        WxUserActionExample.Criteria criteria = example.createCriteria();

        criteria.andDeletedEqualTo(false);

        List<WxUserAction> list = userActionMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            return ResponseUtil.ok(list);
        } else {
            return ResponseUtil.ok();
        }
    }

    //根据条件查询单个
    public Object read(Integer userId) {
        WxUserActionExample example = new WxUserActionExample();
        WxUserActionExample.Criteria criteria = example.createCriteria();

        //是否删除
        criteria.andDeletedEqualTo(false);

        List<WxUserAction> list = userActionMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            return ResponseUtil.ok(list.get(list.size() - 1));
        } else {
            return ResponseUtil.ok();
        }
    }

    //根据id查询
    public Object selectByPrimaryKey(Integer id){
        return  ResponseUtil.ok(userActionMapper.selectByPrimaryKey(id));
    }

    //添加
    public Object add(WxUserAction object) {
        LocalDateTime now = LocalDateTime.now();
        object.setUpdateTime(now);
        object.setAddTime(now);
        object.setDeleted(false);
        return ResponseUtil.ok(userActionMapper.insertSelective(object));
    }

    //编辑
    public Object updateById(Integer userId, WxUserAction object) {
        object.setUpdateTime(LocalDateTime.now());
        return ResponseUtil.ok(userActionMapper.updateByPrimaryKey(object));
    }

    //批量删除
    @Transactional(rollbackFor=Exception.class)
    public Object deleteByIds(Integer[] ids){
        for(Integer id : ids){
            WxUserAction entity = userActionMapper.selectByPrimaryKey(id);
            entity.setDeleted(true);
            userActionMapper.updateByPrimaryKeySelective(entity);
        }
        return ResponseUtil.ok();
    }

    public void action(Long updateTime, Integer userId) {
        try {
            boolean add = true;
            if (updateTime != null && updateTime > 0) {
                LocalDateTime localDateTime = Instant.ofEpochMilli(updateTime).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
                if (DateTimeUtil.isSameDayAndHour(LocalDateTime.now(), localDateTime)) {  // 同一天同一小时的操作不用存
                    add = false;
                }
            }

            if (add) {
                WxUserAction entity = new WxUserAction();
                entity.setUserId(userId);
                add(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
