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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.measure.vo.IotMeasureBookPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.measure.vo.IotMeasureBookRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.measure.vo.IotMeasureBookSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.measure.IotMeasureBookDO;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.measure.IotMeasureBookMapper;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserImportRespVO;
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.*;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_IMPORT_LIST_IS_EMPTY;

/**
 * 计量器具台账 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotMeasureBookServiceImpl implements IotMeasureBookService {

    @Resource
    private IotMeasureBookMapper iotMeasureBookMapper;
    @Autowired
    private DeptService deptService;

    @Override
    public UserImportRespVO importBookList(List<IotMeasureBookRespVO> importBooks) {
        // 1.1 参数校验
        if (CollUtil.isEmpty(importBooks)) {
            throw exception(USER_IMPORT_LIST_IS_EMPTY);
        }
        List<IotMeasureBookDO> bookDOList = new ArrayList<>();
        // 2. 遍历，逐个创建 or 更新
        UserImportRespVO respVO = UserImportRespVO.builder().createUsernames(new ArrayList<>()).updateUsernames(new ArrayList<>()).failureUsernames(new LinkedHashMap<>()).build();
        Long loginUserDeptId = SecurityFrameworkUtils.getLoginUserDeptId();
        importBooks.forEach(importBook -> {
            IotMeasureBookDO iotMeasureBookDO = new IotMeasureBookDO();
            BeanUtils.copyProperties(importBook, iotMeasureBookDO);
            iotMeasureBookDO.setDeleted(false);
            iotMeasureBookDO.setDeptId(loginUserDeptId);
            bookDOList.add(iotMeasureBookDO);
        });
        iotMeasureBookMapper.insertBatch(bookDOList);
        return respVO;
    }

    @Override
    public Long createIotMeasureBook(IotMeasureBookSaveReqVO createReqVO) {
        // 插入
        IotMeasureBookDO iotMeasureBook = BeanUtils.toBean(createReqVO, IotMeasureBookDO.class);
        iotMeasureBook.setMeasureCode("JLQJ"+ DateUtil.format(new Date(), "yyyyMMddHHmmss"));
        iotMeasureBook.setDeleted(false);
        iotMeasureBook.setAlertWarn(false);
        iotMeasureBook.setExpired(false);
        //处理计量器具的检测是否过期
        iotMeasureBookMapper.insert(iotMeasureBook);
        // 返回
        return iotMeasureBook.getId();
    }

    @Override
    public void updateIotMeasureBook(IotMeasureBookSaveReqVO updateReqVO) {
        // 校验存在
        validateIotMeasureBookExists(updateReqVO.getId());
        // 更新
        IotMeasureBookDO updateObj = BeanUtils.toBean(updateReqVO, IotMeasureBookDO.class);
        iotMeasureBookMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotMeasureBook(Long id) {
        // 校验存在
        validateIotMeasureBookExists(id);
        // 删除
        iotMeasureBookMapper.deleteById(id);
    }

    private void validateIotMeasureBookExists(Long id) {
        if (iotMeasureBookMapper.selectById(id) == null) {
            throw exception(new ErrorCode(111,"计量器具台账"));
        }
    }

    @Override
    public IotMeasureBookDO getIotMeasureBook(Long id) {
        return iotMeasureBookMapper.selectById(id);
    }

    @Override
    public PageResult<IotMeasureBookDO> getIotMeasureBookPage(IotMeasureBookPageReqVO pageReqVO) {
        Set<Long> ids = new HashSet<>();
        if (Objects.nonNull(pageReqVO.getDeptId())) {
            ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
            ids.add(pageReqVO.getDeptId());
        }
        return iotMeasureBookMapper.selectPage(pageReqVO,ids);
    }

}
