package com.ssm.manage.service.impl;

import com.ssm.manage.dao.StaffTemporaryDao;
import com.ssm.manage.dao.StaffTemporaryUpdateDao;
import com.ssm.manage.dto.StaffTemporaryDto;
import com.ssm.manage.dto.StaffTemporaryUpdateDto;
import com.ssm.manage.entity.StaffTemporary;
import com.ssm.manage.entity.StaffTemporaryUpdate;
import com.ssm.manage.response.StaffTemporaryUpdateVo;
import com.ssm.manage.service.StaffTemporaryUpdateService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ssm.manage.dao.BaseDao;
import com.ssm.core.impl.BaseDaoImpl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

/**
 * (StaffTemporaryUpdate)表服务实现类
 *
 * @author makejava
 * @since 2024-03-04 09:33:23
 */
@Service("staffTemporaryUpdateService")
public class StaffTemporaryUpdateServiceImpl extends BaseServiceImpl<StaffTemporaryUpdate> implements StaffTemporaryUpdateService {
    @Autowired
    private StaffTemporaryUpdateDao staffTemporaryUpdateDao;
    @Autowired
    private StaffTemporaryDao staffTemporaryDao;


    @Override
    public BaseDao<StaffTemporaryUpdate> getDao() {
        return staffTemporaryUpdateDao;
    }

    @Override
    public PageInfo<StaffTemporaryUpdateVo> selectPage(StaffTemporaryUpdateDto dto) {
        Example example = new Example(StaffTemporaryUpdate.class);
        Example.Criteria criteria = example.createCriteria();
        if (Objects.nonNull(dto.getStartTime())){
            criteria.andBetween(StaffTemporaryUpdateDto.Fields.updateDate,dto.getStartTime(),dto.getEndTime());
        }
        if (Objects.nonNull(dto.getStaffName())){
            criteria.andLike(StaffTemporaryUpdateDto.Fields.staffName,dto.getStaffName());
        }
        if (Objects.nonNull(dto.getStaffNum())){
            criteria.andLike(StaffTemporaryUpdateDto.Fields.staffNum,dto.getStaffNum());
        }
        if (Objects.nonNull(dto.getIdentityCard())){
            criteria.andLike(StaffTemporaryUpdateDto.Fields.identityCard,dto.getIdentityCard());
        }
        if (Objects.nonNull(dto.getMobilePhone())){
            criteria.andLike(StaffTemporaryUpdateDto.Fields.mobilePhone,dto.getMobilePhone());
        }
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize(),"update_date desc");
        List<StaffTemporaryUpdate> list = this.staffTemporaryUpdateDao.selectByExample(example);
        List<StaffTemporaryUpdateVo> staffTemporaryUpdateVos = new ArrayList<>();
            for (StaffTemporaryUpdate temporary : list) {
                StaffTemporaryUpdateVo updateVo = new StaffTemporaryUpdateVo();
                BeanUtils.copyProperties(temporary,updateVo);
                example.clear();
                Example.Criteria exampleCriteria = example.createCriteria();
                exampleCriteria.andEqualTo(StaffTemporaryUpdateDto.Fields.staffTemporaryId,temporary.getStaffTemporaryId());
                exampleCriteria.andGreaterThan(StaffTemporaryUpdateDto.Fields.updateDate,temporary.getUpdateDate());
                PageHelper.startPage(1,1,"update_date asc");
                List<StaffTemporaryUpdate> staffTemporaryUpdates = staffTemporaryUpdateDao.selectByExample(example);
                if (!CollectionUtils.isEmpty(staffTemporaryUpdates)){
                    StaffTemporary staffTemporary = new StaffTemporary();
                    BeanUtils.copyProperties(staffTemporaryUpdates.get(0),staffTemporary);
                    updateVo.setStaffTemporary(staffTemporary);
                    staffTemporaryUpdateVos.add(updateVo);
                    continue;
                }
                StaffTemporary staffTemporary = staffTemporaryDao.selectByPrimaryKey(temporary.getStaffTemporaryId());
                updateVo.setStaffTemporary(staffTemporary);
                staffTemporaryUpdateVos.add(updateVo);
            }
        PageInfo<StaffTemporaryUpdate> pageInfo = new PageInfo<>(list);
        PageInfo<StaffTemporaryUpdateVo> info = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo,info);
        info.setList(staffTemporaryUpdateVos);
        return info;
    }
}
