package com.yuchen.saas.park.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.dto.park.SystemTagReqDTO;
import com.yuchen.saas.park.api.dto.park.SystemTagSubmitReqDTO;
import com.yuchen.saas.park.api.entity.park.PmgBatParkSystemTag;
import com.yuchen.saas.park.api.vo.park.SystemTagVO;
import com.yuchen.saas.park.service.mapper.PmgBatParkSystemTagMapper;
import com.yuchen.saas.park.service.service.IPmgBatParkSystemTagService;
import com.yuchen.saas.park.service.util.PageConverterUtils;
import org.nest.springwrap.core.log.exception.ServiceException;
import org.nest.springwrap.core.mp.base.BaseEntity;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.secure.ContextUser;
import org.nest.springwrap.core.secure.utils.AuthUtil;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 系统标签表 服务实现类
 * </p>
 *
 * @author zhouhanwei
 * @since 2024-11-12
 */
@Service
public class PmgBatParkSystemTagServiceImpl extends BaseServiceImpl<PmgBatParkSystemTagMapper, PmgBatParkSystemTag> implements IPmgBatParkSystemTagService {

    @Override
    public NestPage<SystemTagVO> findSystemTagPage(SystemTagReqDTO reqDTO) {
        ContextUser contextUser = AuthUtil.getUser();
        if (Objects.isNull(contextUser)) {
            throw new ServiceException("用户不存在");
        }
        Page<SystemTagVO> resultPage = new Page<>();
        // 分页数据
        Query query = new Query();
        query.setCurrent(reqDTO.getCurrent());
        query.setSize(reqDTO.getSize());
        // 赋值数据
        LambdaQueryWrapper<PmgBatParkSystemTag> lambdaWrapper = Wrappers.lambdaQuery();
        lambdaWrapper.eq(PmgBatParkSystemTag::getCustomerId, contextUser.getCustomerManageId());
        lambdaWrapper.eq(Func.isNotEmpty(reqDTO.getBroadCategory()), PmgBatParkSystemTag::getBroadCategory, reqDTO.getBroadCategory());
        lambdaWrapper.like(Func.isNotEmpty(reqDTO.getTagName()), PmgBatParkSystemTag::getTagName, reqDTO.getTagName());
        lambdaWrapper.eq(Func.isNotEmpty(reqDTO.getStatus()), PmgBatParkSystemTag::getStatus, reqDTO.getStatus());
        lambdaWrapper.orderByDesc(BaseEntity::getCreateTime);
        IPage<PmgBatParkSystemTag> page = page(Condition.getPage(query),lambdaWrapper);
        List<PmgBatParkSystemTag> systemTagList = page.getRecords();

        // 数据列表
        List<SystemTagVO> resultList = new ArrayList<>();
        for (PmgBatParkSystemTag pmgBatParkSystemTag : systemTagList) {
            SystemTagVO vo = new SystemTagVO();
            BeanUtil.copyProperties(pmgBatParkSystemTag, vo);
            resultList.add(vo);
        }
        resultPage.setTotal(page.getTotal());
        resultPage.setSize(page.getSize());
        resultPage.setCurrent(page.getCurrent());
        resultPage.setRecords(resultList);
        return PageConverterUtils.convert(resultPage);
    }

    @Override
    public SystemTagVO getSystemTagById(Long systemTagId) {
        SystemTagVO vo = new SystemTagVO();
        PmgBatParkSystemTag pmgBatParkSystemTag = getById(systemTagId);
        if (Objects.isNull(pmgBatParkSystemTag)) {
            throw new ServiceException("系统标签数据不存在");
        }
        BeanUtil.copyProperties(pmgBatParkSystemTag, vo);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addSystemTag(SystemTagSubmitReqDTO reqDTO) {
        PmgBatParkSystemTag pmgBatParkSystemTag = new PmgBatParkSystemTag();
        BeanUtil.copyProperties(reqDTO, pmgBatParkSystemTag);
        // 判断去重
        validRepeat(pmgBatParkSystemTag);
        pmgBatParkSystemTag.setCustomerId(AuthUtil.getUser().getCustomerManageId());
        pmgBatParkSystemTag.setCreateOrgUser(AuthUtil.getUser().getOrgUserId());
        return save(pmgBatParkSystemTag);
    }

    private void validRepeat(PmgBatParkSystemTag pmgBatParkSystemTag) {
        int count = count(new LambdaQueryWrapper<PmgBatParkSystemTag>()
                .eq(PmgBatParkSystemTag::getTagName, pmgBatParkSystemTag.getTagName())
                .eq(PmgBatParkSystemTag::getCustomerId, pmgBatParkSystemTag.getCustomerId())
                .notIn(Objects.nonNull(pmgBatParkSystemTag.getId()), BaseEntity::getId, pmgBatParkSystemTag.getId()));
        if (count > 0) {
            throw new ServiceException("系统标签名称不能重复");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editSystemTag(SystemTagSubmitReqDTO reqDTO) {
        PmgBatParkSystemTag pmgBatParkSystemTag = getById(reqDTO.getId());
        if (Objects.isNull(pmgBatParkSystemTag)) {
            throw new ServiceException("系统标签数据不存在");
        }
        BeanUtil.copyProperties(reqDTO, pmgBatParkSystemTag);
        // 判断去重
        validRepeat(pmgBatParkSystemTag);
        pmgBatParkSystemTag.setUpdateOrgUser(AuthUtil.getUser().getOrgUserId());
        return updateById(pmgBatParkSystemTag);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeSystemTag(Long systemTagId) {
        PmgBatParkSystemTag pmgBatParkSystemTag = getById(systemTagId);
        if (Objects.isNull(pmgBatParkSystemTag)) {
            throw new ServiceException("系统标签数据不存在");
        }
        return removeById(systemTagId);
    }
}
