package cn.iocoder.yudao.module.pms.service.iotprojecttaskschedule;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
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.iotoperationmeetingattrs.vo.IotOperationMeetingAttrsPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojecttaskschedule.vo.IotProjectTaskSchedulePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojecttaskschedule.vo.IotProjectTaskScheduleSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotoperationmeetingattrs.IotOperationMeetingAttrsDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotprojecttask.IotProjectTaskDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotprojecttaskattrs.IotTaskAttrModelProperty;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotprojecttaskschedule.IotProjectTaskScheduleDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotoperationmeetingattrs.IotOperationMeetingAttrsMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.iotprojecttask.IotProjectTaskMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.iotprojecttaskschedule.IotProjectTaskScheduleMapper;
import com.google.common.collect.ImmutableMap;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_PROJECT_TASK_SCHEDULE_NOT_EXISTS;

/**
 * 项目任务时间表/施工进度 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotProjectTaskScheduleServiceImpl implements IotProjectTaskScheduleService {

    @Resource
    private IotProjectTaskScheduleMapper iotProjectTaskScheduleMapper;

    @Resource
    private IotProjectTaskMapper iotProjectTaskMapper;

    @Resource
    private IotOperationMeetingAttrsMapper iotOperationMeetingAttrsMapper;

    @Override
    public Long createIotProjectTaskSchedule(IotProjectTaskScheduleSaveReqVO createReqVO) {
        // 插入
        IotProjectTaskScheduleDO iotProjectTaskSchedule = BeanUtils.toBean(createReqVO, IotProjectTaskScheduleDO.class);
        iotProjectTaskScheduleMapper.insert(iotProjectTaskSchedule);
        // 返回
        return iotProjectTaskSchedule.getId();
    }

    @Override
    public void updateIotProjectTaskSchedule(IotProjectTaskScheduleSaveReqVO updateReqVO) {
        // 校验存在
        validateIotProjectTaskScheduleExists(updateReqVO.getId());
        // 更新
        IotProjectTaskScheduleDO updateObj = BeanUtils.toBean(updateReqVO, IotProjectTaskScheduleDO.class);
        iotProjectTaskScheduleMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotProjectTaskSchedule(Long id) {
        // 校验存在
        validateIotProjectTaskScheduleExists(id);
        // 删除
        iotProjectTaskScheduleMapper.deleteById(id);
    }

    private void validateIotProjectTaskScheduleExists(Long id) {
        if (iotProjectTaskScheduleMapper.selectById(id) == null) {
            throw exception(IOT_PROJECT_TASK_SCHEDULE_NOT_EXISTS);
        }
    }

    @Override
    public IotProjectTaskScheduleDO getIotProjectTaskSchedule(Long id) {
        return iotProjectTaskScheduleMapper.selectById(id);
    }

    @Override
    public PageResult<IotProjectTaskScheduleDO> getIotProjectTaskSchedulePage(IotProjectTaskSchedulePageReqVO pageReqVO) {
        return iotProjectTaskScheduleMapper.selectPage(pageReqVO);
    }

    @Override
    public Long saveTaskSchedule(List<IotProjectTaskScheduleSaveReqVO> schedules) {
        // 先删除已有的进度计划记录 再新增
        if (CollUtil.isNotEmpty(schedules)) {
            Long taskId = schedules.get(0).getTaskId();
            iotProjectTaskScheduleMapper.deleteByMap(ImmutableMap.of(
                    "task_id", taskId
            ));
            List<IotProjectTaskScheduleDO> tobeAddedSchedules = new ArrayList<>();
            // 使用 Java8 Stream 转换，并忽略 id
            tobeAddedSchedules = schedules.stream()
                    .map(vo -> {
                        IotProjectTaskScheduleDO entity = new IotProjectTaskScheduleDO();
                        // 拷贝属性时忽略 id
                        BeanUtil.copyProperties(vo, entity, "id");
                        return entity;
                    })
                    .collect(Collectors.toList());
            // 批量保存
            if (CollUtil.isNotEmpty(tobeAddedSchedules)) {
                iotProjectTaskScheduleMapper.insertBatch(tobeAddedSchedules);
            }
        }
        return 1l;
    }

    @Override
    public List<IotProjectTaskScheduleDO> getIotProjectTaskSchedules(IotProjectTaskSchedulePageReqVO pageReqVO) {
        return iotProjectTaskScheduleMapper.selectList(pageReqVO);
    }

    @Override
    public List<IotTaskAttrModelProperty> scheduleExtProperties(IotProjectTaskSchedulePageReqVO pageReqVO) {
        Long taskId = pageReqVO.getTaskId();
        if (ObjUtil.isEmpty(taskId)) {
            return Collections.emptyList();
        }
        IotProjectTaskDO task = iotProjectTaskMapper.selectById(taskId);
        if (ObjUtil.isEmpty(task)) {
            return Collections.emptyList();
        }
        Long deptId = task.getDeptId();
        if (ObjUtil.isEmpty(deptId)) {
            return Collections.emptyList();
        }
        // 根据任务井所属的 公司id 查询工作量扩展属性
        IotOperationMeetingAttrsPageReqVO reqVO = new IotOperationMeetingAttrsPageReqVO();
        reqVO.setDeptId(deptId);
        reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<IotOperationMeetingAttrsDO> pageAttrs = iotOperationMeetingAttrsMapper.selectPage(reqVO);
        List<IotOperationMeetingAttrsDO> workloadAttrs = pageAttrs.getList();
        if (CollUtil.isEmpty(workloadAttrs)) {
            return Collections.emptyList();
        }
        List<IotTaskAttrModelProperty> workloadProperties = new ArrayList<>();
        workloadAttrs.forEach(attr -> {
            // 提取 ‘计划xx’ 属性
            IotTaskAttrModelProperty property = attr.getExtProperty();
            String defaultValue = property.getDefaultValue();
            if (ObjUtil.isNotEmpty(property) && property.getName().contains("计划") && StrUtil.isBlank(defaultValue)) {
                workloadProperties.add(attr.getExtProperty());
            }
        });
        return workloadProperties;
    }

}