package cn.hanghui.outapi.platform.tenant.service.impl;

import cn.hanghui.outapi.platform.common.entity.rest.RestResult;
import cn.hanghui.outapi.platform.common.utils.ClassUtil;
import cn.hanghui.outapi.platform.tenant.entity.config.maintenance.device.DeviceModelVo;
import cn.hanghui.outapi.platform.tenant.entity.config.maintenance.purchaser.PurchaserDto;
import cn.hanghui.outapi.platform.tenant.entity.config.maintenance.purchaser.PurchaserSearch;
import cn.hanghui.outapi.platform.tenant.entity.config.maintenance.purchaser.PurchaserVo;
import cn.hanghui.outapi.platform.tenant.entity.dto.ProveDeviceVo;
import cn.hanghui.outapi.platform.tenant.entity.po.DeviceModel;
import cn.hanghui.outapi.platform.tenant.entity.po.ProveDevice;
import cn.hanghui.outapi.platform.tenant.entity.po.Purchaser;
import cn.hanghui.outapi.platform.tenant.mapper.PurchaserMapper;
import cn.hanghui.outapi.platform.tenant.service.PurchaserService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 购买方管理 服务实现类
 * </p>
 *
 * @author wxg
 * @since 2023-12-27
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PurchaserServiceImpl implements PurchaserService {

    private final PurchaserMapper purchaserMapper;

    @Override
    public Boolean edit(PurchaserDto dto) {
        ClassUtil.throwBusinessException(StrUtil.isNotBlank(dto.getCompanyHeadPhone()) && !Validator.isMobile(dto.getCompanyHeadPhone()),"手机号格式有误");
        Long purchaserId = dto.getPurchaserId();
        if(purchaserId == null){
            Purchaser purchaser = BeanUtil.copyProperties(dto,Purchaser.class);
            purchaserMapper.insert(purchaser);
        }else {
            Purchaser base = purchaserMapper.selectById(purchaserId);
            ClassUtil.throwBusinessException(ObjectUtil.isNull(base),"购买方不存在");
            BeanUtil.copyProperties(dto,base);
            purchaserMapper.updateById(base);
        }
        return true;
    }

    @Override
    public List<PurchaserVo> purchaserList() {
        LambdaQueryWrapper<Purchaser> wrapper = new LambdaQueryWrapper<>();
        //查询的基础条件
        wrapper.eq(Purchaser::getIsDeleted,Boolean.FALSE);
        wrapper.orderByDesc(Purchaser::getId);
        List<Purchaser> deviceModelList = purchaserMapper.selectList(wrapper);

        return deviceModelList.stream().map(p->{
            PurchaserVo item = BeanUtil.copyProperties(p,PurchaserVo.class);
            item.setPurchaserId(p.getId());
            return item;
        }).collect(Collectors.toList());
    }

    @Override
    public IPage<PurchaserVo> purchaserPage(PurchaserSearch dto) {
        Page<Purchaser> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        LambdaQueryWrapper<Purchaser> wrapper = this.getWrapper(dto);
        IPage<Purchaser> pageList = purchaserMapper.selectPage(page, wrapper);
        return pageList.convert(t -> {
            PurchaserVo item = BeanUtil.copyProperties(t, PurchaserVo.class);
            item.setPurchaserId(t.getId());
            return item;
        });
    }
    private LambdaQueryWrapper<Purchaser> getWrapper(PurchaserSearch dto) {
        LambdaQueryWrapper<Purchaser> wrapper = new LambdaQueryWrapper<>();
        //查询的传参条件
        wrapper.like(StrUtil.isNotBlank(dto.getCompanyName()),Purchaser::getCompanyName,dto.getCompanyName());
        wrapper.like(StrUtil.isNotBlank(dto.getCompanyHeadName()),Purchaser::getCompanyHeadName,dto.getCompanyHeadName());
        wrapper.eq(StrUtil.isNotBlank(dto.getCompanyHeadPhone()),Purchaser::getCompanyHeadPhone,dto.getCompanyHeadPhone());
        //查询的基础条件
        wrapper.eq(Purchaser::getIsDeleted,Boolean.FALSE);
        wrapper.orderByDesc(Purchaser::getId);
        return wrapper;
    }
    @Override
    public Boolean purchaserDelete(Long purchaserId) {
        Purchaser purchaser = purchaserMapper.selectById(purchaserId);
        ClassUtil.throwBusinessException(ObjectUtil.isNull(purchaser),"购买方不存在");
        ClassUtil.throwBusinessException(purchaser.getIsDeleted(),"购买方已删除");
        purchaser.setIsDeleted(Boolean.TRUE);
        purchaserMapper.updateById(purchaser);
        return Boolean.TRUE;
    }
}
