package com.xindazhou.business.service.community.banner;

import com.xindazhou.business.controller.admin.community.banner.vo.BannerPageReqVO;
import com.xindazhou.business.controller.admin.community.banner.vo.BannerRespVO;
import com.xindazhou.business.controller.admin.community.banner.vo.BannerSaveReqVO;
import com.xindazhou.business.convert.community.banner.BannerConvert;
import com.xindazhou.business.dal.community.dataobject.banner.BannerDO;
import com.xindazhou.business.dal.community.mysql.banner.BannerMapper;
import com.xindazhou.framework.common.exception.util.ServiceExceptionUtil;
import com.xindazhou.framework.common.pojo.PageResult;
import com.xindazhou.framework.mybatis.core.query.LambdaQueryWrapperX;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import static com.xindazhou.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_FOUND;

/**
 * Banner Service 实现类
 *
 * @author xindazhou
 */
@Service("communityBannerServiceImpl")
@Validated
@Slf4j
public class BannerServiceImpl implements BannerService {

    @Resource
    private BannerMapper bannerMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createBanner(BannerSaveReqVO createReqVO) {
        // 生成Banner编号
        Long bannerId = System.currentTimeMillis();
        
        // 插入Banner
        BannerDO banner = BannerConvert.INSTANCE.convert(createReqVO);
        banner.setBannerId(bannerId);
        banner.setStatus(0); // 0-未上架
        
        bannerMapper.insert(banner);
        return banner.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBanner(BannerSaveReqVO updateReqVO) {
        // 校验存在
        validateBannerExists(updateReqVO.getId());
        
        // 更新
        BannerDO updateObj = BannerConvert.INSTANCE.convert(updateReqVO);
        updateObj.setId(updateReqVO.getId());
        bannerMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBanner(Long id) {
        // 校验存在
        validateBannerExists(id);
        // 删除
        bannerMapper.deleteById(id);
    }

    @Override
    public BannerRespVO getBanner(Long id) {
        BannerDO banner = validateBannerExists(id);
        return BannerConvert.INSTANCE.convert(banner);
    }

    @Override
    public PageResult<BannerRespVO> getBannerPage(BannerPageReqVO pageReqVO) {
        LambdaQueryWrapperX<BannerDO> queryWrapper = new LambdaQueryWrapperX<BannerDO>()
                .likeIfPresent(BannerDO::getDescription, pageReqVO.getDescription())
                .eqIfPresent(BannerDO::getStatus, pageReqVO.getStatus())
                .betweenIfPresent(BannerDO::getOnlineTime, pageReqVO.getOnlineTimeStart(), pageReqVO.getOnlineTimeEnd())
                .orderByDesc(BannerDO::getSort)
                .orderByDesc(BannerDO::getCreateTime);
        PageResult<BannerDO> pageResult = bannerMapper.selectPage(pageReqVO, queryWrapper);
        return BannerConvert.INSTANCE.convertPage(pageResult);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBannerStatus(Long id, Integer status) {
        validateBannerExists(id);
        
        BannerDO updateObj = new BannerDO();
        updateObj.setId(id);
        updateObj.setStatus(status);
        
        // 根据状态更新上架/下架时间
        if (status == 1) {
            // 上架
            updateObj.setOnlineTime(LocalDateTime.now());
        } else if (status == 2) {
            // 下架
            updateObj.setOfflineTime(LocalDateTime.now());
        }
        
        bannerMapper.updateById(updateObj);
    }

    @Override
    public List<com.xindazhou.business.controller.app.community.banner.vo.BannerRespVO> getBannerList() {
        // 查询已上架的Banner
        List<BannerDO> list = bannerMapper.selectList(new LambdaQueryWrapperX<BannerDO>()
                .eq(BannerDO::getStatus, 1) // 1-已上架
                .orderByAsc(BannerDO::getSort)
                .orderByDesc(BannerDO::getCreateTime));
        
        // 转换为移动端VO
        return list.stream().map(banner -> {
            com.xindazhou.business.controller.app.community.banner.vo.BannerRespVO vo = 
                    new com.xindazhou.business.controller.app.community.banner.vo.BannerRespVO();
            vo.setId(banner.getId());
            vo.setImageUrl(banner.getImageUrl());
            vo.setDescription(banner.getDescription());
            vo.setPushType(banner.getPushType());
            vo.setPushTarget(banner.getPushTarget());
            vo.setSort(banner.getSort());
            return vo;
        }).collect(java.util.stream.Collectors.toList());
    }

    private BannerDO validateBannerExists(Long id) {
        BannerDO banner = bannerMapper.selectById(id);
        if (banner == null) {
            throw ServiceExceptionUtil.exception(NOT_FOUND, "Banner不存在");
        }
        return banner;
    }

}

