package org.dromara.elevator.service.impl;

import cn.hutool.core.util.ObjectUtil;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StreamUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.mybatis.helper.DataBaseHelper;
import org.dromara.elevator.domain.EElevatorInfo;
import org.dromara.elevator.domain.EWorkData;
import org.dromara.elevator.domain.vo.EElevatorInfoVo;
import org.dromara.elevator.mapper.EElevatorInfoMapper;
import org.dromara.system.domain.SysDept;
import org.dromara.system.mapper.SysDeptMapper;
import org.springframework.stereotype.Service;
import org.dromara.elevator.domain.bo.EElevatorInsuranceBo;
import org.dromara.elevator.domain.vo.EElevatorInsuranceVo;
import org.dromara.elevator.domain.EElevatorInsurance;
import org.dromara.elevator.mapper.EElevatorInsuranceMapper;
import org.dromara.elevator.service.IEElevatorInsuranceService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Objects;

/**
 * 电梯和保险信息Service业务层处理
 *
 * @author zy
 * @date 2024-11-22
 */
@RequiredArgsConstructor
@Service
public class EElevatorInsuranceServiceImpl implements IEElevatorInsuranceService {

    private final EElevatorInsuranceMapper baseMapper;
    private final SysDeptMapper deptMapper;
    private final EElevatorInfoMapper elevatorInfoMapper;

    /**
     * 查询电梯和保险信息
     *
     * @param id 主键
     * @return 电梯和保险信息
     */
    @Override
    public EElevatorInsuranceVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询电梯和保险信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 电梯和保险信息分页列表
     */
    @Override
    public TableDataInfo<EElevatorInsuranceVo> queryPageList(EElevatorInsuranceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EElevatorInsurance> lqw = buildQueryWrapper(bo);
        Page<EElevatorInsuranceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的电梯和保险信息列表
     *
     * @param bo 查询条件
     * @return 电梯和保险信息列表
     */
    @Override
    public List<EElevatorInsuranceVo> queryList(EElevatorInsuranceBo bo) {
        LambdaQueryWrapper<EElevatorInsurance> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EElevatorInsurance> buildQueryWrapper(EElevatorInsuranceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EElevatorInsurance> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getElevatorNum()), EElevatorInsurance::getElevatorNum, bo.getElevatorNum());
        lqw.like(StringUtils.isNotBlank(bo.getElevatorName()), EElevatorInsurance::getElevatorName, bo.getElevatorName());
        lqw.eq(StringUtils.isNotBlank(bo.getElevatorAddress()), EElevatorInsurance::getElevatorAddress, bo.getElevatorAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getElevatorId()), EElevatorInsurance::getElevatorId, bo.getElevatorId());
        lqw.eq(bo.getInsuranceId() != null, EElevatorInsurance::getInsuranceId, bo.getInsuranceId());
        lqw.like(StringUtils.isNotBlank(bo.getInsuranceName()), EElevatorInsurance::getInsuranceName, bo.getInsuranceName());
        lqw.like(StringUtils.isNotBlank(bo.getClientName()), EElevatorInsurance::getClientName, bo.getClientName());
        lqw.eq(bo.getPremium() != null, EElevatorInsurance::getPremium, bo.getPremium());
        lqw.eq(StringUtils.isNotBlank(bo.getContract()), EElevatorInsurance::getContract, bo.getContract());
        lqw.ge(Objects.nonNull(bo.getExpirationTimeStart()), EElevatorInsurance::getExpirationTime, bo.getExpirationTimeStart());
        lqw.le(Objects.nonNull(bo.getExpirationTimeEnd()), EElevatorInsurance::getExpirationTime, bo.getExpirationTimeEnd());
        lqw.eq(StringUtils.isNotBlank(bo.getContractNum()), EElevatorInsurance::getContractNum, bo.getContractNum());
        // lqw.eq(bo.getDeptId() != null, EElevatorInsurance::getDeptId, bo.getDeptId());
        // lqw.eq(bo.getExpirationTime() != null, EElevatorInsurance::getExpirationTime, bo.getExpirationTime());

        lqw.and(ObjectUtil.isNotNull(bo.getDeptId()), w -> {
            List<SysDept> deptList = deptMapper.selectList(new LambdaQueryWrapper<SysDept>()
                .select(SysDept::getDeptId)
                .apply(DataBaseHelper.findInSet(bo.getDeptId(), "ancestors")));
            List<Long> ids = StreamUtils.toList(deptList, SysDept::getDeptId);
            ids.add(bo.getDeptId());
            w.in(EElevatorInsurance::getDeptId, ids);
        }).orderByDesc(EElevatorInsurance::getCreateTime);

        return lqw;
    }

    /**
     * 新增电梯和保险信息
     *
     * @param bo 电梯和保险信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(EElevatorInsuranceBo bo) {
        EElevatorInsurance add = MapstructUtils.convert(bo, EElevatorInsurance.class);
        add.setDeptId(bo.getInsuranceId());
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改电梯和保险信息
     *
     * @param bo 电梯和保险信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(EElevatorInsuranceBo bo) {
        EElevatorInsurance update = MapstructUtils.convert(bo, EElevatorInsurance.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EElevatorInsurance entity){
        //TODO 做一些数据校验,如唯一约束
        LambdaQueryWrapper<EElevatorInfo> num = Wrappers.lambdaQuery();
        num.eq(StringUtils.isNotBlank(entity.getElevatorNum()), EElevatorInfo::getElevatorNum, entity.getElevatorNum());
        EElevatorInfoVo elevatorInfo = elevatorInfoMapper.selectVoOne(num);
        if (Objects.isNull(elevatorInfo)) {
            throw new ServiceException("未查询到该电梯信息!");
        }
        entity.setElevatorName(elevatorInfo.getElevatorName());
        entity.setElevatorAddress(elevatorInfo.getElevatorAddress());
        entity.setElevatorId(String.valueOf(elevatorInfo.getId()));

    }

    /**
     * 校验并批量删除电梯和保险信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
