package com.jeeplus.test.changes.dimission.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.test.changes.dimission.domain.ChangesDimission;
import com.jeeplus.test.changes.dimission.domain.ChangesDimissionFiles;
import com.jeeplus.test.changes.dimission.domain.ChangesDimissionHandover;
import com.jeeplus.test.changes.dimission.domain.ChangesDimissionRea;
import com.jeeplus.test.changes.dimission.mapper.ChangesDimissionHandoverMapper;
import com.jeeplus.test.changes.dimission.mapper.ChangesDimissionMapper;
import com.jeeplus.test.changes.dimission.service.dto.ChangesDimissionDTO;
import com.jeeplus.test.changes.dimission.service.dto.ChangesDimissionFilesDTO;
import com.jeeplus.test.changes.dimission.service.dto.ChangesDimissionHandoverDTO;
import com.jeeplus.test.changes.dimission.service.dto.ChangesDimissionReaDTO;
import com.jeeplus.test.changes.dimission.service.mapstruct.ChangesDimissionFilesWrapper;
import com.jeeplus.test.changes.dimission.service.mapstruct.ChangesDimissionHandoverWrapper;
import com.jeeplus.test.changes.dimission.service.mapstruct.ChangesDimissionWrapper;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional(rollbackFor = Exception.class)
public class ChangesDimissionHandoverService extends ServiceImpl<ChangesDimissionHandoverMapper, ChangesDimissionHandover> {

    @Resource
    private ChangesDimissionHandoverMapper changesDimissionHandoverMapper;
    @Resource
    private ChangesDimissionMapper changesDimissionMapper;

    public IPage<ChangesDimissionHandoverDTO> findList(Page<ChangesDimissionHandoverDTO> page, QueryWrapper<ChangesDimissionHandover> queryWrapper){
        queryWrapper.eq("cdh.del_flag","0").eq("cdh.parent_id","0");
        return changesDimissionHandoverMapper.findList(page,queryWrapper);
    }

    public void saveHandover(ChangesDimissionHandoverDTO changesDimissionHandoverDTO) {

        if(ObjectUtil.isNotEmpty(changesDimissionHandoverDTO)){
            if(StringUtils.isNotBlank(changesDimissionHandoverDTO.getCdId())){
                this.remove(new QueryWrapper<ChangesDimissionHandover>().lambda().eq(ChangesDimissionHandover::getCdId,changesDimissionHandoverDTO.getCdId()));
            }
            if(CollectionUtil.isNotEmpty(changesDimissionHandoverDTO.getHandoverDTOList())){
                List<ChangesDimissionHandover> changesDimissionHandoverList = new ArrayList<>();
                changesDimissionHandoverDTO.getHandoverDTOList().stream().forEach(item->{
                    ChangesDimissionHandover changesDimissionHandover = ChangesDimissionHandoverWrapper.INSTANCE.toEntity(item);
                    changesDimissionHandover.setParentId("0");
                    if(StringUtils.isNotBlank(changesDimissionHandoverDTO.getCdId())){
                        changesDimissionHandover.setCdId(changesDimissionHandoverDTO.getCdId());
                    }
                    changesDimissionHandover.setId(null);
                    this.save(changesDimissionHandover);
                    if(CollectionUtil.isNotEmpty(item.getHandoverDTOList())){
                        item.getHandoverDTOList().stream().forEach(item2->{
                            ChangesDimissionHandover handover = ChangesDimissionHandoverWrapper.INSTANCE.toEntity(item2);
                            handover.setParentId(changesDimissionHandover.getId());
                            if(StringUtils.isNotBlank(changesDimissionHandoverDTO.getCdId())){
                                handover.setCdId(changesDimissionHandoverDTO.getCdId());
                            }
                            handover.setId(null);
                            changesDimissionHandoverList.add(handover);
                        });
                    }
                });
                this.saveBatch(changesDimissionHandoverList);
                ChangesDimission changesDimission = new ChangesDimission();
                changesDimission.setId(changesDimissionHandoverDTO.getCdId());
                changesDimission.setHandoverStatus(changesDimissionHandoverDTO.getHandoverStatus());
                changesDimissionMapper.updateById(changesDimission);
            }
        }
    }

    public List<ChangesDimissionHandoverDTO> queryByCdId(String id) {

        List<ChangesDimissionHandoverDTO> changesDimissionHandoverDTOList = changesDimissionHandoverMapper.queryByCdId(id);

        return changesDimissionHandoverDTOList;
    }

    /**
     * 根据离职id来修改离职交接状态 handoverStatus的值
     * @param changesDimissionDTO
     */
    public void updateByCdId(ChangesDimissionDTO changesDimissionDTO){
        ChangesDimission changesDimission = new ChangesDimission();
        changesDimission.setHandoverStatus(changesDimissionDTO.getHandoverStatus());
        this.changesDimissionMapper.update(changesDimission,new QueryWrapper<ChangesDimission>().lambda().eq(ChangesDimission::getId,changesDimissionDTO.getId()));
    }

}
