package com.njcn.device.pms.service.majornetwork.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.pms.enums.PmsDeviceResponseEnum;
import com.njcn.device.pms.mapper.majornetwork.TractionStationMapper;
import com.njcn.device.pms.pojo.dto.PmsTractionStationDTO;
import com.njcn.device.pms.pojo.param.PmsTractionStationParam;
import com.njcn.device.pms.pojo.param.TractionStationParam;
import com.njcn.device.pms.pojo.po.TractionStation;
import com.njcn.device.pms.service.majornetwork.ITractionStationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.web.factory.PageFactory;
import com.njcn.web.pojo.param.BaseParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2022-10-14
 */
@Service
@RequiredArgsConstructor
public class TractionStationServiceImpl extends ServiceImpl<TractionStationMapper, TractionStation> implements ITractionStationService {

    private final TractionStationMapper tractionStationMapper;

    /**
     * 查询牵引站列表
     * @return
     */
    @Override
    public List<TractionStation> getTractionStationList() {
        LambdaQueryWrapper<TractionStation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TractionStation::getStatus,DataStateEnum.ENABLE.getCode())
                .orderByDesc(TractionStation::getCreateTime);
        return this.list();
    }

    /**
     * 分页查询牵引站列表
     *
     * @param baseParam
     * @return
     */
    @Override
    public Page<TractionStation> getTractionStationPageList(BaseParam baseParam) {
        LambdaQueryWrapper<TractionStation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TractionStation::getStatus,DataStateEnum.ENABLE.getCode())
                .and(StrUtil.isNotBlank(baseParam.getSearchValue()),
                        i->i.like(TractionStation::getName,baseParam.getSearchValue())
                        .or()
                        .like(TractionStation::getOrgName,baseParam.getSearchValue())
                        .or()
                        .like(TractionStation::getOperationName,baseParam.getSearchValue())
                        .or()
                        .like(TractionStation::getPowerName,baseParam.getSearchValue()))
                .orderByDesc(TractionStation::getCreateTime);
        return this.page(new Page<>(PageFactory.getPageNum(baseParam),PageFactory.getPageSize(baseParam)),lambdaQueryWrapper);
    }

    /**
     * 根据ID查询牵引站信息
     *
     * @param tractionStationId
     * @return
     */
    @Override
    public TractionStation getTractionStationById(String tractionStationId) {
        return this.getById(tractionStationId);
    }

    /**
     * 新增牵引站信息
     *
     * @author hany
     * @date 2022/10/28
     */
    @Override
    public boolean add(TractionStationParam tractionStationParam) {
        checkName(tractionStationParam, true);
        TractionStation tractionStation = new TractionStation();
        BeanUtils.copyProperties(tractionStationParam, tractionStation);
        //设为正常状态
        tractionStation.setStatus(DataStateEnum.ENABLE.getCode());
        tractionStation.setCreatedDate(LocalDate.parse(tractionStationParam.getCreatedDate()));
        return this.save(tractionStation);
    }

    /**
     * 修改牵引站信息
     *
     * @author hany
     * @date 2022/10/28
     */
    @Override
    public boolean update(TractionStationParam updateParam) {
        checkName(updateParam, false);
        TractionStation tractionStation = new TractionStation();
        BeanUtils.copyProperties(updateParam, tractionStation);
        tractionStation.setCreatedDate(LocalDate.parse(updateParam.getCreatedDate()));
        return this.updateById(tractionStation);
    }

    /**
     * 删除
     *
     * @param ids
     */
    @Override
    public boolean delete(List<String> ids) {
        return this.lambdaUpdate().set(TractionStation::getStatus, DataStateEnum.DELETED.getCode()).in(TractionStation::getId, ids).update();
    }

    /**
     * 获取牵引站详细信息
     *
     * @param param 条件参数
     * @return java.util.List<com.njcn.device.pms.pojo.dto.PmsTractionStationDTO>
     * @author yzh
     * @date 2022/11/8
     */
    @Override
    public List<PmsTractionStationDTO> getPmsTractionStationInfo(PmsTractionStationParam param) {
        return tractionStationMapper.getPmsTractionStationInfo(param);
    }

    /**
     * 名称重复校验
     */
    private void checkName(TractionStationParam tractionStationParam, boolean flag) {
        LambdaQueryWrapper<TractionStation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TractionStation::getName, tractionStationParam.getName());
        //修改
        if (!flag) {
            if (tractionStationParam instanceof TractionStationParam) {
                lambdaQueryWrapper.ne(TractionStation::getId, tractionStationParam.getId());
            }
        }
        int result = this.count(lambdaQueryWrapper);
        //大于等于1个则表示重复
        if (result >= 1) {
            throw new BusinessException(PmsDeviceResponseEnum.MODEL_NAME_REPEAT);
        }

    }

}
