package com.lantone.daqe.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantone.common.enums.IsDeleteEnum;
import com.lantone.common.util.BeanUtil;
import com.lantone.common.util.StringUtil;
import com.lantone.common.util.SysUserUtils;
import com.lantone.daqe.dto.GetOfficialCapacityPageDTO;
import com.lantone.daqe.entity.OfficialCapacity;
import com.lantone.daqe.facade.base.OfficialCapacityFacade;
import com.lantone.daqe.vo.DelOfficialCapacityByIdVO;
import com.lantone.daqe.vo.GetOfficialCapacityPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description: 职务职称变更记录-业务处理类
 * @author: zhanghang
 * @time: 2022/2/28 17:47
 */

@Component
public class OfficialCapacityManagementFacade {

    @Autowired
    private OfficialCapacityFacade officialCapacityFacade;

    /**
     * 获取职务职称变更记录分页列表数据
     *
     * @param getOfficialCapacityPageVO
     * @return: 职务职称变更记录
     */
    public IPage<GetOfficialCapacityPageDTO> getOfficialCapacityPage(GetOfficialCapacityPageVO getOfficialCapacityPageVO) {
        Long currentHospitalId = SysUserUtils.getCurrentHospitalId();
        getOfficialCapacityPageVO.setHospitalId(currentHospitalId);
        Page<GetOfficialCapacityPageDTO> getOfficialCapacityPageDTOPage = new Page<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startDate = null;
        String endDate = null;
        if (getOfficialCapacityPageVO.getChangeTimeStart() != null) {
            startDate = sdf.format(getOfficialCapacityPageVO.getChangeTimeStart());
        }
        if (getOfficialCapacityPageVO.getChangeTimeEnd() != null) {
            endDate = sdf.format(getOfficialCapacityPageVO.getChangeTimeStart());
        }
        QueryWrapper<OfficialCapacity> officialCapacityQueryWrapper = new QueryWrapper<>();
        officialCapacityQueryWrapper.eq(getOfficialCapacityPageVO.getHospitalId() != null, "hospital_id", getOfficialCapacityPageVO.getHospitalId());
        officialCapacityQueryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey());
        officialCapacityQueryWrapper.ge(StringUtil.isNotBlank(startDate), "change_time", getOfficialCapacityPageVO.getChangeTimeStart());
        officialCapacityQueryWrapper.le(StringUtil.isNotBlank(endDate), "change_time", getOfficialCapacityPageVO.getChangeTimeEnd());
        officialCapacityQueryWrapper.like(StringUtil.isNotBlank(getOfficialCapacityPageVO.getType()), "type", getOfficialCapacityPageVO.getType());
        officialCapacityQueryWrapper.like(StringUtil.isNotBlank(getOfficialCapacityPageVO.getName()), "name", getOfficialCapacityPageVO.getName());
        officialCapacityQueryWrapper.like(StringUtil.isNotBlank(getOfficialCapacityPageVO.getDoctorName()), "doctor_name", getOfficialCapacityPageVO.getDoctorName());
        officialCapacityQueryWrapper.like(StringUtil.isNotBlank(getOfficialCapacityPageVO.getDoctorCode()), "doctor_code", getOfficialCapacityPageVO.getDoctorCode());
        officialCapacityQueryWrapper.like(StringUtil.isNotBlank(getOfficialCapacityPageVO.getDeptName()), "dept_name", getOfficialCapacityPageVO.getDeptName());
        Page<OfficialCapacity> officialCapacityPage = new Page<>(getOfficialCapacityPageVO.getCurrent(), getOfficialCapacityPageVO.getSize());
        officialCapacityFacade.page(officialCapacityPage, officialCapacityQueryWrapper);
        BeanUtil.copyProperties(officialCapacityPage, getOfficialCapacityPageDTOPage);
        List<GetOfficialCapacityPageDTO> getOfficialCapacityPageDTOList = BeanUtil.listCopyTo(officialCapacityPage.getRecords(), GetOfficialCapacityPageDTO.class);

        getOfficialCapacityPageDTOPage.setRecords(getOfficialCapacityPageDTOList);
        return getOfficialCapacityPageDTOPage;
    }


    /**
     * 根据ID删除职务职称变更记录
     *
     * @param delOfficialCapacityByIdVO
     * @return: 是否成功
     */
    public Boolean delOfficialCapacityPage(DelOfficialCapacityByIdVO delOfficialCapacityByIdVO) {
        OfficialCapacity officialCapacity = new OfficialCapacity();
        officialCapacity.setIsDeleted(IsDeleteEnum.Y.getKey());
        officialCapacity.setGmtModified(new Date());
        String currentUserId = SysUserUtils.getCurrentPrincipleIdStr();
        officialCapacity.setCreator(currentUserId);
        Long[] id = delOfficialCapacityByIdVO.getId();
        return officialCapacityFacade.update(officialCapacity, new QueryWrapper<OfficialCapacity>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .in("id", id)
        );
    }

}