package com.ruoyi.cms.service.policy.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.cms.domain.policy.PolicyInfo;
import com.ruoyi.cms.domain.policy.PolicyInfoQuery;
import com.ruoyi.cms.mapper.policy.PolicyInfoMapper;
import com.ruoyi.cms.service.policy.IPolicyInfoService;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 政策信息Service实现
 * @author kiro
 * @date 2026-01-11
 */
@Slf4j
@Service
public class PolicyInfoServiceImpl implements IPolicyInfoService {

    @Autowired
    private PolicyInfoMapper policyInfoMapper;

    @Override
    public TableDataInfo getPortalList(PolicyInfoQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<PolicyInfo> list = policyInfoMapper.selectPolicyInfoList(query);
        PageInfo<PolicyInfo> pageInfo = new PageInfo<>(list);

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setMsg("操作成功");
        rspData.setRows(list);
        rspData.setTotal(pageInfo.getTotal());
        return rspData;
    }

    @Override
    public PolicyInfo getDetail(Long id) {
        return policyInfoMapper.selectPolicyInfoById(id);
    }

    @Override
    public int addViewNum(Long id) {
        return policyInfoMapper.updateViewNum(id);
    }

    // ========== CMS后台管理接口实现 ==========

    @Override
    public List<PolicyInfo> selectList(PolicyInfoQuery query) {
        return policyInfoMapper.selectPolicyInfoList(query);
    }

    @Override
    public PolicyInfo selectById(Long id) {
        return policyInfoMapper.selectPolicyInfoById(id);
    }

    @Override
    public int insert(PolicyInfo policyInfo) {
        policyInfo.setCreateBy(SecurityUtils.getUsername());
        policyInfo.setCreateTime(String.valueOf(new Date()));
        if (policyInfo.getViewNum() == null) {
            policyInfo.setViewNum(0);
        }
        return policyInfoMapper.insertPolicyInfo(policyInfo);
    }

    @Override
    public int update(PolicyInfo policyInfo) {
        policyInfo.setUpdateBy(SecurityUtils.getUsername());
        policyInfo.setUpdateTime(String.valueOf(new Date()));
        return policyInfoMapper.updatePolicyInfo(policyInfo);
    }

    @Override
    public int deleteByIds(Long[] ids) {
        return policyInfoMapper.deletePolicyInfoByIds(ids);
    }
}
