package com.xjrsoft.module.material.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.module.material.dto.AddMaterialTaskAssignDto;
import com.xjrsoft.module.material.dto.MaterialAssignUserDto;
import com.xjrsoft.module.material.dto.MaterialTaskAssignApprovedDto;
import com.xjrsoft.module.material.entity.MaterialTask;
import com.xjrsoft.module.material.entity.MaterialTaskAssign;
import com.xjrsoft.module.material.mapper.MaterialTaskAssignMapper;
import com.xjrsoft.module.material.mapper.MaterialTaskMapper;
import com.xjrsoft.module.material.service.IMaterialTaskAssignService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@AllArgsConstructor
public class MaterialTaskAssignServiceImpl   extends MPJBaseServiceImpl<MaterialTaskAssignMapper, MaterialTaskAssign> implements IMaterialTaskAssignService {
    private final MaterialTaskAssignMapper materialTaskAssignMapper;
    private final MaterialTaskMapper materialTaskMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(AddMaterialTaskAssignDto addMaterialTaskAssignDto) {

        if (addMaterialTaskAssignDto.getUserList() != null && addMaterialTaskAssignDto.getUserList().size() > 0) {

            Integer count = 0;
            List<MaterialTaskAssign> materialTypeAssignList = materialTaskAssignMapper.selectList(Wrappers.<MaterialTaskAssign>query().lambda()
                    .eq(MaterialTaskAssign::getMaterialTaskId, addMaterialTaskAssignDto.getMaterialTaskId()));
            for (MaterialAssignUserDto addMaterialTypeAssignUserDto : addMaterialTaskAssignDto.getUserList()) {
                Optional<MaterialTaskAssign> materialTaskAssignAny = materialTypeAssignList.stream().filter(item -> item.getUserId().equals(addMaterialTypeAssignUserDto.getUserId())).findAny();
                if (!materialTaskAssignAny.isPresent()) {
                    MaterialTaskAssign materialTaskAssign = new MaterialTaskAssign();
                    materialTaskAssign.setMaterialTaskId(addMaterialTaskAssignDto.getMaterialTaskId());
                    materialTaskAssign.setUserId(addMaterialTypeAssignUserDto.getUserId());
                    materialTaskAssign.setUserType(addMaterialTypeAssignUserDto.getUserType());
                    materialTaskAssign.setUserNumber(addMaterialTypeAssignUserDto.getUserNumber());
                    materialTaskAssign.setSendMessage(addMaterialTypeAssignUserDto.getSendMessage());
                    materialTaskAssign.setTransferMaterialTaskId(0L);
                    materialTaskAssign.setExecutiveStatus(1);
                    materialTaskAssign.setSendMessageStatus(0);
                    materialTaskAssign.setStatus(1);
                    materialTaskAssign.setTransferAssignPerson(0);
                    materialTaskAssign.setTransferSubmitedPerson(0);
                    materialTaskAssignMapper.insert(materialTaskAssign);
                    count = count + 1;

                }
            }

            if (count > 0) {
                MaterialTask materialTask = materialTaskMapper.selectByIdDeep(addMaterialTaskAssignDto.getMaterialTaskId());
                materialTask.setAssignPerson(materialTask.getAssignPerson() + count);
                materialTaskMapper.updateById(materialTask);
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        List<MaterialTaskAssign> materialTaskAssignList = materialTaskAssignMapper.selectList(Wrappers.lambdaQuery(MaterialTaskAssign.class).in(MaterialTaskAssign::getId, ids));
        for (MaterialTaskAssign materialTaskAssign : materialTaskAssignList) {
            MaterialTask materialTask = materialTaskMapper.selectByIdDeep(materialTaskAssign.getMaterialTaskId());
            int count = materialTaskAssignMapper.deleteBatchIds(ids);
            materialTask.setAssignPerson(materialTask.getAssignPerson() - count);
            materialTaskMapper.updateById(materialTask);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean approved(List<MaterialTaskAssignApprovedDto> dtos) {
        MaterialTask materialTask = null;
        for (MaterialTaskAssignApprovedDto dto : dtos) {
            MaterialTaskAssign materialTaskAssign = materialTaskAssignMapper.selectByIdDeep(dto.getId());
            if (materialTaskAssign != null && materialTaskAssign.getStatus() == 2) {
                if (materialTask == null) {
                    materialTask = materialTaskMapper.selectById(materialTaskAssign.getMaterialTaskId());
                }
                if (materialTask != null) {
                    materialTaskAssign.setOpinion(dto.getOpinion());
                    if (dto.getStatus() == 1) {
                        materialTaskAssign.setStatus(3);
                        //materialTaskAssign.setStatus(dto.getStatus() == 1 ? 3 : 4);
//                        if (dto.getExecutiveStatus() == 1) {
//                            materialTask.setOnTimePerson(materialTask.getOnTimePerson() + 1);
//                            materialTaskAssign.setExecutiveStatus(2);
//                        } else {
//                            materialTask.setTimeDelay(materialTask.getTimeDelay() + 1);
//                            materialTaskAssign.setExecutiveStatus(3);
//                        }
                    }
                    if (dto.getStatus() == 2) {//重做
                        materialTaskAssign.setStatus(4);
                    }

                    materialTaskAssignMapper.updateById(materialTaskAssign);
                }
            }
        }
        if (materialTask != null) {
            materialTaskMapper.updateById(materialTask);
        }
        return true;
    }
}
