package com.pj.tb_port_news;

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

import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pj.current.dto.PCLoginUserInfo;
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.so.SoMap;
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) {
        PCLoginUserInfo userInfo = StpUserUtil.getPCLoginInfo();
        t.setIsRelease(0)
                .setCreateBy(userInfo.getLoginId())
                .setCreateName(userInfo.getLoginName())
                .setCreateTime(new Date());
        save(t);
    }

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

    /**
     * 改
     */
    void update(TbPortNews t) {
        PCLoginUserInfo userInfo = StpUserUtil.getPCLoginInfo();
        t.setUpdateBy(userInfo.getLoginId())
                .setUpdateName(userInfo.getLoginName())
                .setUpdateTime(new Date());
        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) {

        PCLoginUserInfo userInfo = StpUserUtil.getPCLoginInfo();
        // 获取口岸咨询
        TbPortNews tbPortNews = tbPortNewsMapper.selectById(id);
        // 口岸咨询不存在抛异常
        if (Objects.isNull(tbPortNews)) {
            throw new RuntimeException("口岸咨询不存在");
        }
        // 口岸咨询被禁用抛异常
        tbPortNews.setIsRelease(isRelease).setReleaseTime(new Date());
        tbPortNews.setUpdateBy(userInfo.getLoginId()).setUpdateName(userInfo.getLoginName()).setUpdateTime(new Date());

        return tbPortNewsMapper.updateById(tbPortNews);
    }


    /**
     * APP端获取最新的5条口岸资讯
     *
     * @author loovi
     * @date
     */
    public AjaxJson getNewestList(Long limit) {
        QueryWrapper<TbPortNews> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .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 getPortNewsDetails(Long id) {
        TbPortNews tbPortNews = tbPortNewsMapper.selectById(id);
        if (tbPortNews != null && tbPortNews.getIsRelease() == 1) {
            tbPortNews.setReadCount(tbPortNews.getReadCount() + 1);
            tbPortNewsMapper.updateById(tbPortNews);
            return AjaxJson.getSuccessData(tbPortNews);
        }
        return AjaxJson.getError("资讯已下架");
    }
}
