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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonus.canteen.core.supply.api.DrpAuthorityApi;
import com.bonus.canteen.core.supply.domain.DrpSupplierDeliver;
import com.bonus.canteen.core.supply.dto.DrpSupplierDeliverAddDTO;
import com.bonus.canteen.core.supply.dto.DrpSupplierDeliverDTO;
import com.bonus.canteen.core.supply.dto.DrpSupplierDeliverDeleteDTO;
import com.bonus.canteen.core.supply.dto.DrpSupplierDeliverEditDTO;
import com.bonus.canteen.core.supply.mapper.DrpSupplierDeliverMapper;
import com.bonus.canteen.core.supply.service.DrpSupplierDeliverService;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.utils.id.Id;
import com.bonus.common.security.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.invoke.SerializedLambda;
import java.util.List;

@Service
public class DrpSupplierDeliverServiceImpl implements DrpSupplierDeliverService {
   private static final Logger log = LoggerFactory.getLogger(DrpSupplierDeliverServiceImpl.class);
   @Resource
   private DrpSupplierDeliverMapper drpSupplierDeliverMapper;
   @Resource
   private DrpAuthorityApi drpAuthorityApi;

   public List<DrpSupplierDeliver> page(DrpSupplierDeliverDTO content) {
      Long supplierAuthority = this.drpAuthorityApi.listSupplierAuthority();
//      List<DrpSupplierDeliver>  list = this.drpSupplierDeliverMapper.selectPage(resultPage, ((LambdaQueryWrapper)((LambdaQueryWrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(DrpSupplierDeliver.class).eq(CharSequenceUtil.isNotEmpty(content.getDeliveryPhone()), DrpSupplierDeliver::getDeliveryPhone, SM4EncryptUtils.sm4Encryptbyconfig(content.getDeliveryPhone()))).eq(CharSequenceUtil.isNotEmpty(content.getDeliveryMan()), DrpSupplierDeliver::getDeliveryMan, content.getDeliveryMan())).eq(supplierAuthority != -1L, DrpSupplierDeliver::getSupplierId, supplierAuthority)).and(CharSequenceUtil.isNotBlank(content.getKeyword()), (x) -> {
//         ((LambdaQueryWrapper)((LambdaQueryWrapper)x.like(DrpSupplierDeliver::getDeliveryMan, content.getKeyword())).or()).like(DrpSupplierDeliver::getDeliveryPhone, SM4EncryptUtils.sm4Encryptbyconfig(content.getKeyword()));
//      }));
      List<DrpSupplierDeliver>  list = this.drpSupplierDeliverMapper.selectList(Wrappers.lambdaQuery(DrpSupplierDeliver.class).eq(CharSequenceUtil.isNotEmpty(content.getDeliveryPhone()), DrpSupplierDeliver::getDeliveryPhone, content.getDeliveryPhone()).eq(CharSequenceUtil.isNotEmpty(content.getDeliveryMan()), DrpSupplierDeliver::getDeliveryMan, content.getDeliveryMan()).eq(supplierAuthority != -1L, DrpSupplierDeliver::getSupplierId, supplierAuthority).and(CharSequenceUtil.isNotBlank(content.getKeyword()), (x) -> {
          x.like(DrpSupplierDeliver::getDeliveryMan, content.getKeyword()).or().like(DrpSupplierDeliver::getDeliveryPhone, content.getKeyword());
      }));
      return list;
   }

   public void add(DrpSupplierDeliverAddDTO content) {
      Long count = this.drpSupplierDeliverMapper.selectCount(Wrappers.lambdaQuery(DrpSupplierDeliver.class).eq(DrpSupplierDeliver::getDeliveryMan, content.getDeliveryMan()).eq(DrpSupplierDeliver::getDeliveryPhone, content.getDeliveryPhone()));
      if (count > 0L) {
         throw new ServiceException("送货人信息已存在!");
      } else {
         DrpSupplierDeliver drpSupplierDeliver = new DrpSupplierDeliver();
         BeanUtil.copyProperties(content, drpSupplierDeliver, new String[0]);
         drpSupplierDeliver.setDeliveryId(Id.next());
         Long supplierAuthority = this.drpAuthorityApi.listSupplierAuthority();
         drpSupplierDeliver.setSupplierId(content.getSupplierId() != null ? content.getSupplierId() : supplierAuthority);
         drpSupplierDeliver.setCreateBy(SecurityUtils.getUserId()+"");
         this.drpSupplierDeliverMapper.insert(drpSupplierDeliver);
      }
   }

   public void edit(DrpSupplierDeliverEditDTO content) {
      DrpSupplierDeliver drpSupplierDeliver = (DrpSupplierDeliver)this.drpSupplierDeliverMapper.selectById(content.getId());
      BeanUtil.copyProperties(content, drpSupplierDeliver, new String[0]);
      this.drpSupplierDeliverMapper.updateById(drpSupplierDeliver);
   }

   public void remove(DrpSupplierDeliverDeleteDTO content) {
      this.drpSupplierDeliverMapper.deleteById(content.getId());
   }
}
