package com.bonus.canteen.core.supply.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.canteen.core.supply.domain.DrpSupplierQualification;
import com.bonus.canteen.core.supply.dto.DrpSupplierQualificationAddDTO;
import com.bonus.canteen.core.supply.dto.DrpSupplierQualificationEditDTO;
import com.bonus.canteen.core.supply.dto.DrpSupplierQualificationExpireVO;
import com.bonus.canteen.core.supply.dto.DrpSupplierQualificationPageDTO;
import com.bonus.canteen.core.supply.mapper.DrpSupplierQualificationMapper;
import com.bonus.canteen.core.supply.service.DrpSupplierQualificationService;
import com.bonus.canteen.core.supply.vo.DrpSupplierQualificationPageVO;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.constant.DelFlagEnum;
import com.bonus.common.security.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.lang.invoke.SerializedLambda;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class DrpSupplierQualificationServiceImpl extends ServiceImpl<DrpSupplierQualificationMapper, DrpSupplierQualification> implements DrpSupplierQualificationService {
   private static final Logger log = LoggerFactory.getLogger(DrpSupplierQualificationServiceImpl.class);

   public List<DrpSupplierQualificationPageVO> getDrpSupplierQualificationPage(DrpSupplierQualificationPageDTO content) {
      List<DrpSupplierQualificationPageVO> resultPage = new ArrayList();
      LambdaQueryWrapper<DrpSupplierQualification> queryWrapper = new LambdaQueryWrapper();
      queryWrapper.eq(DrpSupplierQualification::getSupplierId, content.getSupplierId());
      queryWrapper.eq(DrpSupplierQualification::getDelFlag, DelFlagEnum.DEL_FALSE.key());
      if (CharSequenceUtil.isNotBlank(content.getQualificationNum())) {
         queryWrapper.eq(DrpSupplierQualification::getQualificationNum, content.getQualificationNum());
      }

      if (CharSequenceUtil.isNotBlank(content.getQualificationName())) {
         queryWrapper.like(DrpSupplierQualification::getQualificationName, content.getQualificationName());
      }

      if (ObjectUtil.isNotEmpty(content.getMainFlag())) {
         queryWrapper.eq(DrpSupplierQualification::getMainFlag, content.getMainFlag());
      }

      resultPage = this.baseMapper.pageSupplierQualification(queryWrapper);
      return resultPage;
   }

   public void addDrpSupplierQualification(DrpSupplierQualificationAddDTO content) {
      String username = SecurityUtils.getLoginUser().getUsername();
      DrpSupplierQualification qualification = new DrpSupplierQualification();
      BeanUtil.copyProperties(content, qualification, new String[0]);
      qualification.setCreateBy(username);
      ((DrpSupplierQualificationMapper)this.baseMapper).insert(qualification);
   }

   public void editDrpSupplierQualification(DrpSupplierQualificationEditDTO content) {
      String username = SecurityUtils.getLoginUser().getUsername();
      DrpSupplierQualification qualification = new DrpSupplierQualification();
      BeanUtil.copyProperties(content, qualification, new String[0]);
      qualification.setUpdateBy(username);
      ((DrpSupplierQualificationMapper)this.baseMapper).update(qualification, (Wrapper)Wrappers.lambdaQuery(DrpSupplierQualification.class).eq(DrpSupplierQualification::getId, content.getId()));
   }

   public void removeBySupplierQualificationId(Long id) {
      if (ObjectUtil.isEmpty(id)) {
         log.info("***[库存中心_供应商资质管理]_删除供应商资质_传入的供应商资质id为空************");
         throw new ServiceException("库存中心_供应商资质管理]_删除供应商资质_传入的供应商资质id为空");
      } else {
       //  ((DrpSupplierQualificationMapper)this.baseMapper).update((Object)null, (Wrapper)((LambdaUpdateWrapper)Wrappers.lambdaUpdate(DrpSupplierQualification.class).eq(DrpSupplierQualification::getId, id)).set(DrpSupplierQualification::getDelFlag, DelFlagEnum.DEL_TRUE.key()));
         this.baseMapper.update(Wrappers.lambdaUpdate(DrpSupplierQualification.class).eq(DrpSupplierQualification::getId, id).set(DrpSupplierQualification::getDelFlag, DelFlagEnum.DEL_TRUE.key()));
      }
   }

   public List<DrpSupplierQualificationExpireVO> expire(DrpSupplierQualificationPageDTO content) {
      List<DrpSupplierQualificationExpireVO> resultPage = new ArrayList();
      resultPage = this.baseMapper.expire(content);
      if (CollUtil.isNotEmpty(resultPage)) {
         resultPage.forEach((d) -> {
            if (d.getValidity() != null) {
               Duration duration = Duration.between(LocalDateTime.now(), d.getValidity().atStartOfDay());
               d.setExpireDays(duration.toDays());
            }
         });
      }
      return resultPage;
   }

}
