package com.zcloud.safetyDutyList.gatewayimpl.tasklist;

import com.zcloud.gbscommon.utils.Tools;
import com.zcloud.safetyDutyList.domain.gateway.tasklist.TaskListIssueGateway;
import com.zcloud.safetyDutyList.domain.model.tasklist.TaskListIssueE;
import com.zcloud.safetyDutyList.persistence.dataobject.tasklist.TaskListIssueDO;
import com.zcloud.safetyDutyList.persistence.repository.tasklist.TaskListIssueRepository;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class TaskListIssueGatewayImpl implements TaskListIssueGateway {

    private final TaskListIssueRepository taskListIssueRepository;

    @Override
    public Boolean add(TaskListIssueE taskListIssueE) {
        if (StringUtils.isEmpty(taskListIssueE.getTaskIssueId())) {
            taskListIssueE.setTaskIssueId(Tools.get32UUID());
        }
        TaskListIssueDO d = new TaskListIssueDO();
        BeanUtils.copyProperties(taskListIssueE, d);
        taskListIssueRepository.save(d);
        taskListIssueE.setId(d.getId());
        return true;
    }

    @Override
    public Boolean update(TaskListIssueE taskListIssueE) {
        TaskListIssueDO d = new TaskListIssueDO();
        BeanUtils.copyProperties(taskListIssueE, d);
        taskListIssueRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean deleteById(Long id) {
        return taskListIssueRepository.removeById(id);
    }

    @Override
    public TaskListIssueE getByTaskIssueId(String taskIssueId) {
        TaskListIssueDO d = taskListIssueRepository.getByTaskIssueId(taskIssueId);
        if (d == null) return null;
        TaskListIssueE e = new TaskListIssueE();
        BeanUtils.copyProperties(d, e);
        return e;
    }

    @Override
    public List<TaskListIssueE> listByTaskListId(String taskListId) {
        List<TaskListIssueDO> doList = taskListIssueRepository.listByTaskListId(taskListId);
        List<TaskListIssueE> eList = new ArrayList<>();
        if (doList != null) {
            for (TaskListIssueDO d : doList) {
                TaskListIssueE e = new TaskListIssueE();
                BeanUtils.copyProperties(d, e);
                eList.add(e);
            }
        }
        return eList;
    }

    @Override
    public List<TaskListIssueE> listExpiredInProgress() {
        List<TaskListIssueDO> doList = taskListIssueRepository.listExpiredInProgress();
        List<TaskListIssueE> eList = new ArrayList<>();
        if (doList != null) {
            for (TaskListIssueDO d : doList) {
                TaskListIssueE e = new TaskListIssueE();
                BeanUtils.copyProperties(d, e);
                eList.add(e);
            }
        }
        return eList;
    }

    @Override
    public List<TaskListIssueE> listIssuedWithPeriod() {
        List<TaskListIssueDO> doList = taskListIssueRepository.listIssuedWithPeriod();
        List<TaskListIssueE> eList = new ArrayList<>();
        if (doList != null) {
            for (TaskListIssueDO d : doList) {
                TaskListIssueE e = new TaskListIssueE();
                BeanUtils.copyProperties(d, e);
                eList.add(e);
            }
        }
        return eList;
    }

    // 清单是否下发
    @Override
    public Boolean isIssued(String taskListId){
        List<TaskListIssueDO> taskListIssueDOList = taskListIssueRepository.listByTaskListId(taskListId);
        return taskListIssueDOList != null && taskListIssueDOList.size() > 0;
    }

    // 清单是否重复下发（同一清单+公司+部门+人员+时间段重叠）
    @Override
    public Boolean isDuplicateIssue(String taskListId, Long executeCorpId, Long executeDepartmentId, Long executeUserId,
                                     java.time.LocalDateTime periodStartTime, java.time.LocalDateTime periodEndTime){
        List<TaskListIssueDO> taskListIssueDOList = taskListIssueRepository.listByIssueInfo(taskListId, executeCorpId, executeDepartmentId, executeUserId, periodStartTime, periodEndTime);
        return taskListIssueDOList != null && taskListIssueDOList.size() > 0;
    }
}
