package com.njcn.advance.service.carrycapacity.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.advance.enums.AdvanceResponseEnum;
import com.njcn.advance.mapper.carrycapacity.CarryCapacityUserPOMapper;
import com.njcn.advance.pojo.carrycapacity.param.CarryCapacityUserParam;
import com.njcn.advance.pojo.carrycapacity.po.CarryCapacityUserPO;
import com.njcn.advance.service.carrycapacity.CarryCapacityUserPOService;
import com.njcn.advance.utils.CheckStringUtil;
import com.njcn.common.pojo.constant.LogInfo;
import com.njcn.common.pojo.constant.PatternRegex;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.harmonic.enums.HarmonicResponseEnum;
import com.njcn.harmonic.pojo.param.ReportTemplateParam;
import com.njcn.harmonic.pojo.po.ExcelRptTemp;
import com.njcn.user.api.UserFeignClient;
import com.njcn.user.pojo.vo.UserVO;
import com.njcn.web.constant.ValidMessage;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;
import java.util.regex.Pattern;

/**
 * 
 * Description:
 * Date: 2024/2/20  11:15【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class CarryCapacityUserPOServiceImpl extends ServiceImpl<CarryCapacityUserPOMapper, CarryCapacityUserPO> implements CarryCapacityUserPOService{
    private final UserFeignClient userFeignClient;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(CarryCapacityUserParam carryCapacityUserParam) {
        if(StringUtils.isBlank(carryCapacityUserParam.getUserName())){
            throw new BusinessException("用户称不能为空");
        }
        checkName(carryCapacityUserParam,false);
        CarryCapacityUserPO carryCapacityUser = new CarryCapacityUserPO();
        BeanUtils.copyProperties(carryCapacityUserParam, carryCapacityUser);
        carryCapacityUser.setStatus(1);
        return this.save(carryCapacityUser);
    }
    /**
     * 检查名称是否已存在
     *
     * @return 结果
     */
    private void checkName(CarryCapacityUserParam carryCapacityUserParam, boolean isUpdate) {
        if(carryCapacityUserParam.getUserName().length()>32){
            throw new BusinessException("超过最大长度");

        }
        CheckStringUtil.checkName(carryCapacityUserParam.getUserName());
        LambdaQueryWrapper<CarryCapacityUserPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //条件组合：where state = 1 and name = ?
        lambdaQueryWrapper.eq(CarryCapacityUserPO::getStatus, DataStateEnum.ENABLE.getCode()).eq(CarryCapacityUserPO::getUserName, carryCapacityUserParam.getUserName());

        //and id <> ?
        if (isUpdate) {
            if (carryCapacityUserParam instanceof CarryCapacityUserParam.CarryCapacityUserUpdateParam) {
                lambdaQueryWrapper.ne(CarryCapacityUserPO::getUserId, ((CarryCapacityUserParam.CarryCapacityUserUpdateParam) carryCapacityUserParam).getUserId());
            }
        }

        //若存在条件数据抛出异常
        int count = this.getBaseMapper().selectCount(lambdaQueryWrapper);
        if (count > 0) {
            throw new BusinessException(AdvanceResponseEnum.USER_NAME_EXIST);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateUser(CarryCapacityUserParam.CarryCapacityUserUpdateParam userUpdateParam) {
        if(StringUtils.isBlank(userUpdateParam.getUserName())){
            throw new BusinessException("用户称不能为空");
        }
        checkName(userUpdateParam,true);
        CarryCapacityUserPO carryCapacityUser = new CarryCapacityUserPO();
        BeanUtils.copyProperties(userUpdateParam, carryCapacityUser);


        return this.updateById(carryCapacityUser);
    }

    @Override
    public IPage<CarryCapacityUserPO> queyDetailUser(CarryCapacityUserParam.CarryCapacityUserPageParam pageParm) {
        Page<CarryCapacityUserPO> returnpage = new Page<> (pageParm.getPageNum ( ), pageParm.getPageSize ( ));
        QueryWrapper<CarryCapacityUserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CarryCapacityUserPO::getStatus,1)
                .eq(StringUtils.isNotBlank(pageParm.getUserId()) ,CarryCapacityUserPO::getUserId,pageParm.getUserId())
                .eq(StringUtils.isNotBlank(pageParm.getVoltage()) ,CarryCapacityUserPO::getVoltage,pageParm.getVoltage())
                .eq(StringUtils.isNotBlank(pageParm.getUserType()) ,CarryCapacityUserPO::getUserType,pageParm.getUserType())
                .in(CollectionUtil.isNotEmpty(pageParm.getUserTypeList()) ,CarryCapacityUserPO::getUserType,pageParm.getUserTypeList())
                .between(StringUtils.isNotBlank(pageParm.getStartTime()) && StringUtils.isNotBlank(pageParm.getEndTime()) ,CarryCapacityUserPO::getCreateTime,pageParm.getStartTime()+" 00:00:00",pageParm.getEndTime()+" 23:59:59").
                orderByDesc(CarryCapacityUserPO::getCreateTime);

        IPage<CarryCapacityUserPO> page = this.page (returnpage, queryWrapper);
        page.getRecords().forEach(temp->{
            UserVO user = userFeignClient.getUserById(temp.getCreateBy()).getData();

            temp.setCreateBy(Objects.isNull(user)? LogInfo.UNKNOWN_USER:user.getName());
        });
        return page;
    }

    @Override
    public CarryCapacityUserPO queyDetailUserById(String userId) {
        return this.lambdaQuery().eq(CarryCapacityUserPO::getUserId,userId).one();
    }
}
