package com.czg.service.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.constants.SystemConstants;
import com.czg.exception.CzgException;
import com.czg.product.dto.ConsGroupDTO;
import com.czg.product.dto.ConsInfoDTO;
import com.czg.product.entity.ConsGroup;
import com.czg.product.entity.ConsInfo;
import com.czg.product.service.ConsGroupService;
import com.czg.sa.StpKit;
import com.czg.service.product.mapper.ConsGroupMapper;
import com.czg.service.product.mapper.ConsInfoMapper;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 耗材分组
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-02-20
 */
@AllArgsConstructor
@Service
public class ConsGroupServiceImpl extends ServiceImpl<ConsGroupMapper, ConsGroup> implements ConsGroupService {

    private final ConsInfoMapper consInfoMapper;

    private QueryWrapper buildQueryWrapper(ConsGroupDTO param) {
        QueryWrapper queryWrapper = PageUtil.buildSortQueryWrapper();
        if (StrUtil.isNotEmpty(param.getName())) {
            queryWrapper.like(ConsGroup::getName, param.getName());
        }
        Long shopId = StpKit.USER.getShopId(0L);
        queryWrapper.eq(ConsGroup::getShopId, shopId);
        queryWrapper.orderBy(ConsGroup::getId, false);
        return queryWrapper;
    }

    @Override
    public Page<ConsGroupDTO> getConsGroupPage(ConsGroupDTO param) {
        QueryWrapper queryWrapper = buildQueryWrapper(param);
        return super.pageAs(PageUtil.buildPage(), queryWrapper, ConsGroupDTO.class);
    }

    @Override
    public List<ConsGroupDTO> getConsGroupList(ConsGroupDTO param) {
        QueryWrapper queryWrapper = buildQueryWrapper(param);
        queryWrapper.eq(ConsGroup::getStatus, SystemConstants.OneZero.ONE);
        return super.listAs(queryWrapper, ConsGroupDTO.class);
    }

    @Override
    public ConsGroupDTO getConsGroupById(Long id) {
        Long shopId = StpKit.USER.getShopId(0L);
        ConsGroupDTO dto = super.getOneAs(query().eq(ConsGroup::getId, id).eq(ConsGroup::getShopId, shopId), ConsGroupDTO.class);
        List<ConsInfoDTO> consInfoList = consInfoMapper.selectListByQueryAs(query().eq(ConsInfo::getConsGroupId, dto.getId()), ConsInfoDTO.class);
        dto.setConsIds(consInfoList.stream().map(ConsInfoDTO::getId).collect(Collectors.toList()));
        dto.setConsInfoList(consInfoList);
        return dto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addConsGroup(ConsGroupDTO dto) {
        Long shopId = StpKit.USER.getShopId(0L);
        boolean exists = super.exists(query().eq(ConsGroup::getName, dto.getName()).eq(ConsGroup::getShopId, shopId));
        if (exists) {
            throw new CzgException("耗材分组已存在");
        }
        ConsGroup entity = BeanUtil.copyProperties(dto, ConsGroup.class);
        //entity.setStatus(StatusEnum.ENABLED.value());
        entity.setShopId(shopId);
        super.save(entity);
        dto.setId(entity.getId());
    }

    @Override
    public void updateConsGroup(ConsGroupDTO dto) {
        Long shopId = StpKit.USER.getShopId(0L);
        dto.setShopId(shopId);
        boolean exists = super.exists(query().eq(ConsGroup::getName, dto.getName()).eq(ConsGroup::getShopId, shopId).ne(ConsGroup::getId, dto.getId()));
        if (exists) {
            throw new CzgException("耗材分组已存在");
        }
        ConsGroup entity = BeanUtil.copyProperties(dto, ConsGroup.class);
        super.updateById(entity);
    }

    @Override
    public void deleteConsGroup(Long id) {
        Long shopId = StpKit.USER.getShopId(0L);
        super.remove(query().eq(ConsGroup::getId, id).eq(ConsGroup::getShopId, shopId));
    }

    @Override
    public void disableConsGroup(Long id) {
        Long shopId = StpKit.USER.getShopId(0L);
        UpdateChain.of(ConsGroup.class)
                .set(ConsGroup::getStatus, SystemConstants.OneZero.ZERO)
                .eq(ConsGroup::getId, id)
                .eq(ConsGroup::getShopId, shopId)
                .update();
    }

    @Override
    public void enableConsGroup(Long id) {
        Long shopId = StpKit.USER.getShopId(0L);
        UpdateChain.of(ConsGroup.class)
                .set(ConsGroup::getStatus,SystemConstants.OneZero.ONE)
                .eq(ConsGroup::getId, id)
                .eq(ConsGroup::getShopId, shopId)
                .update();
    }

}