package cn.iocoder.yudao.module.system.service.attendance.addressgroup;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.addressgroup.vo.AttendanceAddressGroupPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.addressgroup.vo.AttendanceAddressGroupSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.addressgroup.AttendanceAddressGroupDO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.addressgroup.AttendanceAddressGroupItemDO;
import cn.iocoder.yudao.module.system.dal.mysql.attendance.addressgroup.AttendanceAddressGroupItemMapper;
import cn.iocoder.yudao.module.system.dal.mysql.attendance.addressgroup.AttendanceAddressGroupMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

/**
 * 考勤地址组 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class AttendanceAddressGroupServiceImpl implements AttendanceAddressGroupService {

    @Resource
    private AttendanceAddressGroupMapper attendanceAddressGroupMapper;
    @Resource
    private AttendanceAddressGroupItemMapper attendanceAddressGroupItemMapper;

    @Override
    public Long createAttendanceAddressGroup(AttendanceAddressGroupSaveReqVO createReqVO) {
        Long id = createReqVO.getId();
        AttendanceAddressGroupDO attendanceAddressGroup = BeanUtils.toBean(createReqVO, AttendanceAddressGroupDO.class);
        if (id == null) {
            // 插入
            attendanceAddressGroupMapper.insert(attendanceAddressGroup);
            id = attendanceAddressGroup.getId();
        } else {
            attendanceAddressGroupMapper.updateById(attendanceAddressGroup);
        }
        // -- 更新子表
        LambdaQueryWrapper<AttendanceAddressGroupItemDO> lambdaQueryWrapper = new LambdaQueryWrapper<AttendanceAddressGroupItemDO>()
                .eq(AttendanceAddressGroupItemDO::getAddressGroupId, id);
        if (CollectionUtil.isEmpty(createReqVO.getItems())) {
            attendanceAddressGroupItemMapper.delete(lambdaQueryWrapper);
        } else {
            List<AttendanceAddressGroupItemDO> items = BeanUtils.toBean(createReqVO.getItems(), AttendanceAddressGroupItemDO.class);
            for (AttendanceAddressGroupItemDO item : items) {
                item.setAddressGroupId(id);
            }
            List<AttendanceAddressGroupItemDO> oldItems = attendanceAddressGroupItemMapper.selectList(new LambdaQueryWrapper<AttendanceAddressGroupItemDO>()
                    .eq(AttendanceAddressGroupItemDO::getAddressGroupId, id));
            // -- 过滤出所有需要添加的
            List<AttendanceAddressGroupItemDO> saveList = items.stream().filter(a -> a.getId() == null).collect(Collectors.toList());
            if (!CollectionUtil.isEmpty(saveList)) {
                attendanceAddressGroupItemMapper.insertBatch(saveList);
            }
            // -- 过滤出所有需要更新的
            List<AttendanceAddressGroupItemDO> editList = items.stream().filter(a -> a.getId() != null).collect(Collectors.toList());
            List<Long> ids = new ArrayList<>();
            if (!CollectionUtil.isEmpty(editList)) {
                attendanceAddressGroupItemMapper.updateBatch(editList);
                ids = editList.stream().map(AttendanceAddressGroupItemDO::getId).collect(Collectors.toList());
            }
            // -- 过滤出所有需要删除的
            List<Long> oldIds = oldItems.stream().map(AttendanceAddressGroupItemDO::getId).collect(Collectors.toList());
            List<Long> delIds = new ArrayList<>(CollectionUtil.subtract(oldIds, ids));
            if (CollectionUtil.isNotEmpty(delIds)) {
                attendanceAddressGroupItemMapper.deleteBatchIds(delIds);
            }
        }
        // 返回
        return id;
    }

    @Override
    public void updateAttendanceAddressGroup(AttendanceAddressGroupSaveReqVO updateReqVO) {
        // 更新
        AttendanceAddressGroupDO updateObj = BeanUtils.toBean(updateReqVO, AttendanceAddressGroupDO.class);
        attendanceAddressGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteAttendanceAddressGroup(Long id) {
        // 删除
        attendanceAddressGroupMapper.deleteById(id);
    }

    @Override
    public AttendanceAddressGroupDO getAttendanceAddressGroup(Long id) {
        return attendanceAddressGroupMapper.selectById(id);
    }

    @Override
    public PageResult<AttendanceAddressGroupDO> getAttendanceAddressGroupPage(AttendanceAddressGroupPageReqVO pageReqVO) {
        return attendanceAddressGroupMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AttendanceAddressGroupDO> getAllList() {
        return attendanceAddressGroupMapper.selectList();
    }

}
