package jnpf.culture.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import jnpf.base.service.SuperServiceImpl;
import jnpf.culture.mapper.CulturePicSettingMapper;
import jnpf.culture.mapper.CulturePicTempMapper;
import jnpf.culture.service.CulturePicSettingService;
import jnpf.entity.culture.CulturePicSetting;
import jnpf.entity.culture.CulturePicTemp;
import jnpf.exception.HandleException;
import jnpf.model.culture.dto.CulturePicSettingDto;
import jnpf.model.culture.dto.SettingQueryDto;
import jnpf.model.culture.dto.UploadDto;
import jnpf.model.culture.vo.CulturePicSettingVo;
import jnpf.util.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 文化打卡 - 图片配置 服务实现
 *
 * @author yanwenfu
 * @create 2025-12-23
 */
@Service
public class CulturePicSettingServiceImpl extends SuperServiceImpl<CulturePicSettingMapper, CulturePicSetting> implements CulturePicSettingService {

    @Resource
    private CulturePicTempMapper culturePicTempMapper;

    @Override
    public List<CulturePicSettingVo> getPicSettingList(SettingQueryDto queryDto) {

        LambdaQueryWrapper<CulturePicSetting> queryWrapper = new LambdaQueryWrapper<CulturePicSetting>()
                .eq(CulturePicSetting::getDeleteMark, ConstantUtil.NUM_FALSE)
                .like(StringUtil.isNotEmpty(queryDto.getKeyword()), CulturePicSetting::getPicName, queryDto.getKeyword())
                .orderByDesc(CulturePicSetting::getCreatorTime);
        List<CulturePicSetting> list = this.list(queryWrapper);
        if (list.isEmpty()) {
            return List.of();
        }
        return JsonUtil.getJsonToList(list, CulturePicSettingVo.class);
    }

    @Override
    public void updatePicName(String id, CulturePicSettingDto picSettingDto) {

        this.update(new LambdaUpdateWrapper<CulturePicSetting>()
                        .eq(CulturePicSetting::getId, id)
                        .set(CulturePicSetting::getPicName, picSettingDto.getPicName())
                        .set(CulturePicSetting::getLastModifyTime, new Date())
                        .set(CulturePicSetting::getLastModifyUserId, UserProvider.getLoginUserId()));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deletePicSettingBatch(List<String> ids) throws Exception {

        Long dbCount = getDbCount();
        if (dbCount.intValue() - ids.size() < 1) {
            throw new HandleException("最少需保留一张图片，不可全部删除");
        }
        // 删除图片配置
        LambdaUpdateWrapper<CulturePicSetting> updateWrapper = new LambdaUpdateWrapper<CulturePicSetting>()
                .in(CulturePicSetting::getId, ids)
                .set(CulturePicSetting::getDeleteMark, ConstantUtil.NUM_TRUE)
                .set(CulturePicSetting::getDeleteUserId, UserProvider.getLoginUserId())
                .set(CulturePicSetting::getDeleteTime, new Date());
        this.update(null, updateWrapper);
        // 删除图片配置关联的临时图片
        LambdaQueryWrapper<CulturePicTemp> queryWrapper = new LambdaQueryWrapper<CulturePicTemp>()
                .in(CulturePicTemp::getPicId, ids);
        culturePicTempMapper.delete(queryWrapper);
    }

    private Long getDbCount() {
        LambdaQueryWrapper<CulturePicSetting> queryWrapper = new LambdaQueryWrapper<CulturePicSetting>()
                .eq(CulturePicSetting::getDeleteMark, ConstantUtil.NUM_FALSE);
        return this.count(queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void uploadPicSetting(UploadDto uploadDto) throws Exception {

        Long dbCount = getDbCount();
        if (dbCount + uploadDto.getUploadList().size() > 100) {
            throw new HandleException("新增图片配置失败，新增数量超出（100）限制");
        }
        List<CulturePicSetting> list = JsonUtil.getJsonToList(uploadDto.getUploadList(), CulturePicSetting.class);
        list.forEach(v -> {
            v.setId(FtbUtil.getId());
            v.setDeleteMark(ConstantUtil.NUM_FALSE);
            v.setCreatorUserId(UserProvider.getLoginUserId());
            v.setLastModifyUserId(UserProvider.getLoginUserId());
        });
        this.saveBatch(list);
    }

    @Override
    public Integer getCulturePicSettingCount() {

        return getDbCount().intValue();
    }
}
