package com.cf.imes.module.system.service.lable;

import cn.hutool.core.collection.CollectionUtil;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.module.system.controller.admin.label.vo.*;
import com.cf.imes.module.system.dal.dataobject.lable.LabelDO;
import com.cf.imes.module.system.dal.dataobject.machine.MachineDO;
import com.cf.imes.module.system.dal.mysql.label.LabelMapper;
import com.cf.imes.module.system.dal.mysql.machine.MachineMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.getUserOrganId;
import static com.cf.imes.module.system.enums.ErrorCodeConstants.LABEL_NOT_EXISTS;
import static com.cf.imes.module.system.enums.ErrorCodeConstants.MACHINE_USE_LABEL;

/**
 * 标签模板 Service 实现类
 *
 * @author 晨丰科技
 */
@Service
@Validated
@Slf4j
public class LabelServiceImpl implements LabelService {

    @Resource
    private LabelMapper labelMapper;

    @Resource
    private MachineMapper machineMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createLabel(LabelSaveReqVO createReqVO) {
        // 插入
        LabelDO Label = BeanUtils.toBean(createReqVO, LabelDO.class);
        labelMapper.insert(Label);
        // 返回
        return Label.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateLabel(LabelSaveReqVO updateReqVO) {
        // 校验存在
        validateLabelExists(updateReqVO.getId());
        // 更新
        LabelDO updateObj = BeanUtils.toBean(updateReqVO, LabelDO.class);
        labelMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteLabel(Long id) {
        // 校验存在
        LabelDO LabelDO = labelMapper.selectById(id);
        if (LabelDO == null) {
            throw exception(LABEL_NOT_EXISTS);
        }
        List<MachineDO> machineDOS = machineMapper.selectList(new LambdaQueryWrapperX<MachineDO>()
                        .select(MachineDO::getId)
                        .eq(MachineDO::getLabelId, id)
        );
        if(CollectionUtil.isNotEmpty(machineDOS)) {
            throw exception(MACHINE_USE_LABEL);
        }
        // 删除标签模板表
        labelMapper.deleteById(id);

    }

    private void validateLabelExists(Long id) {
        if (labelMapper.selectById(id) == null) {
            throw exception(LABEL_NOT_EXISTS);
        }
    }

    @Override
    public LabelRespVO getLabel(Long id) {
        LabelDO LabelDO = labelMapper.selectById(id);
        LabelRespVO LabelRespVO = BeanUtils.toBean(LabelDO, LabelRespVO.class);
        return LabelRespVO;
    }

    @Override
    public PageResult<LabelDO> getLabelPage(LabelPageReqVO pageReqVO) {
        return labelMapper.selectPage(pageReqVO);
    }



    @Override
    public Map<String, List<LabelRespVO>> getGroupList(Long organId) {
        List<LabelDO> LabelDOS = labelMapper.selectList(new LambdaQueryWrapperX<LabelDO>().eqIfPresent(LabelDO::getOrganId, organId));
        List<LabelRespVO> LabelRespVOS = BeanUtils.toBean(LabelDOS, LabelRespVO.class);
        return LabelRespVOS.stream().collect(Collectors.groupingBy(LabelRespVO::getType));
    }

    @Override
    public List<LabelDO> list(String type) {
        return labelMapper.selectList(new LambdaQueryWrapperX<LabelDO>()
                .eq(LabelDO::getOrganId,getUserOrganId())
                .eqIfPresent(LabelDO::getType, type));

//        return labelMapper.selectList(new LambdaQueryWrapperX<LabelDO>()
//                        .eqIfPresent(LabelDO::getType, type)
//                        .eq(LabelDO::getOrganId,getUserOrganId()))
//                .stream().map( m -> m.setTemplate(JsonUtils.unzipString(m.getTemplate()))).collect(Collectors.toList());
    }


}