package com.pj.tb_port_news;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.extra.cglib.CglibUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pj.current.satoken.StpUserUtil;
import com.pj.enummj.DeleteStatus;
import com.pj.enummj.ReleaseStatus;
import com.pj.tb_port_news.vo.TbPortNewsVo;
import com.pj.utils.cache.RedisUtil;
import com.pj.utils.so.SoMap;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.pj.utils.sg.*;

/**
 * Service: tb_port_news -- 口岸咨询
 *
 * @author loovi
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TbPortNewsService extends ServiceImpl<TbPortNewsMapper, TbPortNews> implements IService<TbPortNews> {

    /**
     * 底层 Mapper 对象
     */
    @Autowired
    TbPortNewsMapper tbPortNewsMapper;

    /**
     * 增
     */
    void add(TbPortNews t) {
        save(t);
    }

    /**
     * 删
     */
    void delete(Long id) {
        removeById(id);
    }

    /**
     * 改
     */
    void update(TbPortNews t) {
        updateById(t);

    }

    /**
     * 查
     */
    TbPortNews getById(Long id) {
        return super.getById(id);
    }

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    List<TbPortNews> getList(SoMap so) {
        return tbPortNewsMapper.getList(so);
    }

    /**
     * 更新发布状态
     *
     * @author loovi
     * @date
     */
    int updateRelease(Long id, int isRelease) {

        // 获取当前登录者信息
        String str = RedisUtil.get("pc:" + StpUtil.getLoginIdAsString());
        JSONObject jsonObject = JSONUtil.parseObj(str);
        String loginId = (String) jsonObject.get("loginId");
        String loginName = (String) jsonObject.get("loginName");
        // 获取口岸咨询
        TbPortNews tbPortNews = tbPortNewsMapper.selectById(id);
        // 口岸咨询不存在抛异常
        if (Objects.isNull(tbPortNews)) {
            throw new RuntimeException("口岸咨询不存在");
        }
        // 口岸咨询被禁用抛异常
        if (tbPortNews.getStatus() == DeleteStatus.DELETE_STATUS_OFF.getCode()) {
            throw new RuntimeException("口岸咨询已被禁用");
        }
        // 设置口岸咨询发布状态
        tbPortNews.setIsRelease(isRelease);
        // 设置口岸咨询发布时间
        tbPortNews.setReleaseTime(new Date());
        // 设置口岸咨询更新者id
        tbPortNews.setUpdateBy(loginId);
        // 设置口岸咨询更新者名称
        tbPortNews.setUpdateName(loginName);
        // 更新口岸咨询
        int line = tbPortNewsMapper.updateById(tbPortNews);
        return line;
    }


    /**
     * APP端获取最新的5条口岸资讯
     *
     * @author loovi
     * @date
     */
    public AjaxJson getNewestList(Long limit) {
        QueryWrapper<TbPortNews> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1).eq("is_release", ReleaseStatus.RELEASE_STATUS_PUBLISH.getCode()).orderByDesc("release_time").last("LIMIT " + limit);
        return AjaxJson.getSuccessData(list(queryWrapper));
    }

    /**
     * APP端获取口岸资讯列表
     *
     * @author loovi
     * @date
     */
    public AjaxJson getPortNewsList() {
        // 构造条件查询资讯列表
        QueryWrapper<TbPortNews> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", DeleteStatus.DELETE_STATUS_ON.getCode());
        // 执行查询
        List<TbPortNews> list = tbPortNewsMapper.selectList(queryWrapper);
        // 将结果列表拷贝至VO列表
        List<TbPortNewsVo> resList = CglibUtil.copyList(list, TbPortNewsVo::new);
        // 返回获取的最新口岸资讯
        return AjaxJson.getSuccess("口岸资讯列表", resList);
    }

    /**
     * APP端获取口岸资讯详情
     *
     * @author loovi
     * @date
     */
    public AjaxJson getPortNewsDetails(Long id) {
        TbPortNews tbPortNews = tbPortNewsMapper.selectById(id);
        if (tbPortNews != null && tbPortNews.getStatus() == 1) {
            tbPortNews.setReadCount(tbPortNews.getReadCount() + 1);
            tbPortNewsMapper.updateById(tbPortNews);
            return AjaxJson.getSuccessData(tbPortNews);
        }
        return AjaxJson.getError();
    }
}
