package cn.iocoder.yudao.module.pms.service.qhse.soc;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.soc.vo.IotSocSummaryPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.soc.vo.IotSocSummarySaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.soc.IotSocSourceDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.soc.IotSocSummaryDO;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.soc.IotSocSourceMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.soc.IotSocSummaryMapper;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * SOC卡汇总 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotSocSummaryServiceImpl implements IotSocSummaryService {

    @Resource
    private IotSocSummaryMapper iotSocSummaryMapper;
    @Autowired
    private DeptApi deptApi;
    @Autowired
    private IotSocSourceMapper iotSocSourceMapper;
    @Autowired
    private DeptService deptService;

    @Override
    public Long createIotSocSummary(IotSocSummarySaveReqVO createReqVO) {
        // 插入
        IotSocSummaryDO iotSocSummary = BeanUtils.toBean(createReqVO, IotSocSummaryDO.class);
        iotSocSummary.setSocClass(String.join(",", createReqVO.getSocClasses()));
        String collect = createReqVO.getSocClasses().stream().map(e -> {
            IotSocSourceDO iotSocSourceDO = iotSocSourceMapper.selectById(e);
            if (Objects.nonNull(iotSocSourceDO)) {
                return iotSocSourceDO.getName();
            } else {
                return "";
            }
        }).collect(Collectors.joining(","));
        iotSocSummary.setClassName(collect);
//        Long loginUserDeptId = SecurityFrameworkUtils.getLoginUserDeptId();
//        String loginUserNickname = SecurityFrameworkUtils.getLoginUserNickname();
        iotSocSummary.setUserName(createReqVO.getUserName());
        if (Objects.nonNull(createReqVO.getDeptId())) {
            iotSocSummary.setDeptId(createReqVO.getDeptId());
            DeptRespDTO dept = deptApi.getDept(createReqVO.getDeptId());
            if (Objects.isNull(dept)) {throw new ServiceException(new ErrorCode(3,"不存在"));}
            iotSocSummary.setDeptName(dept.getName());
            Long parentId = dept.getParentId();
            if (Objects.nonNull(parentId)) {
                DeptRespDTO dept1 = deptApi.getDept(parentId);
                if (Objects.nonNull(dept1)) {
                    iotSocSummary.setProject(parentId);
                    iotSocSummary.setProjectName(dept1.getName());
                }
            }
        }
        if (Objects.nonNull(iotSocSummary.getSocClass())) {
            IotSocSourceDO iotSocSourceDO = iotSocSourceMapper.selectById(iotSocSummary.getSocClass());
            if (Objects.isNull(iotSocSourceDO)) {throw new ServiceException(new ErrorCode(2,"不存在类别"));}
            iotSocSummary.setClassName(iotSocSourceDO.getName());
        }
        iotSocSummary.setDeleted(false);
        iotSocSummaryMapper.insert(iotSocSummary);
        // 返回
        return iotSocSummary.getId();
    }

    @Override
    public void updateIotSocSummary(IotSocSummarySaveReqVO updateReqVO) {
        // 校验存在
        validateIotSocSummaryExists(updateReqVO.getId());
        // 更新
        IotSocSummaryDO updateObj = BeanUtils.toBean(updateReqVO, IotSocSummaryDO.class);
        updateObj.setSocClass(String.join(",", updateReqVO.getSocClasses()));
        String collect = updateReqVO.getSocClasses().stream().map(e -> {
            IotSocSourceDO iotSocSourceDO = iotSocSourceMapper.selectById(e);
            if (Objects.nonNull(iotSocSourceDO)) {
                return iotSocSourceDO.getName();
            } else {
                return "";
            }
        }).collect(Collectors.joining(","));
        updateObj.setClassName(collect);
        iotSocSummaryMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotSocSummary(Long id) {
        // 校验存在
        validateIotSocSummaryExists(id);
        // 删除
        iotSocSummaryMapper.deleteById(id);
    }

    private void validateIotSocSummaryExists(Long id) {
        if (iotSocSummaryMapper.selectById(id) == null) {
            throw exception(new ErrorCode(2,"不存在"));
        }
    }

    @Override
    public IotSocSummaryDO getIotSocSummary(Long id) {
        return iotSocSummaryMapper.selectById(id);
    }

    @Override
    public PageResult<IotSocSummaryDO> getIotSocSummaryPage(IotSocSummaryPageReqVO pageReqVO) {
        Set<Long> ids = new HashSet<>();
        if (Objects.nonNull(pageReqVO.getDeptId())) {
            ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
            ids.add(pageReqVO.getDeptId());
        }
        return iotSocSummaryMapper.selectPage(pageReqVO, ids);
    }

}