package com.fjhx.jxst.service.sales.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.customer.entity.customer.po.Customer;
import com.fjhx.customer.service.customer.CustomerService;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.jxst.entity.sales.dto.SalesContractDto;
import com.fjhx.jxst.entity.sales.dto.SalesContractSelectDto;
import com.fjhx.jxst.entity.sales.po.SalesContract;
import com.fjhx.jxst.entity.sales.po.SalesContractDetails;
import com.fjhx.jxst.entity.sales.vo.SalesContractDetailsVo;
import com.fjhx.jxst.entity.sales.vo.SalesContractVo;
import com.fjhx.jxst.mapper.sales.SalesContractMapper;
import com.fjhx.jxst.service.sales.SalesContractDetailsService;
import com.fjhx.jxst.service.sales.SalesContractService;
import com.fjhx.jxst.utils.CodeEnum;
import com.fjhx.wms.entity.stock.vo.StockVo;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 销售合同 服务实现类
 * </p>
 *
 * @author
 * @since 2023-05-16
 */
@Service
public class SalesContractServiceImpl extends ServiceImpl<SalesContractMapper, SalesContract> implements SalesContractService {

    @Autowired
    private SalesContractDetailsService salesContractDetailsService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ProductInfoService productInfoService;

    @Override
    public Page<SalesContractVo> getPage(SalesContractSelectDto dto) {
        IWrapper<SalesContract> wrapper = getWrapper();
        if (ObjectUtil.isNotEmpty(dto.getKeyword())) {
            List<Customer> list = customerService.list(q -> q.like(Customer::getName, dto.getKeyword()));
            if (ObjectUtil.isNotEmpty(list)) {
                List<Long> ids = list.stream().map(Customer::getId).collect(Collectors.toList());
                wrapper.and(q -> q.in(SalesContract::getCustomerId, ids).or().like(SalesContractVo::getContractAmount, dto.getKeyword()));
            }else{
                wrapper.and(q -> q.like(SalesContractVo::getContractAmount, dto.getKeyword()));
            }
        }
        wrapper.orderByDesc("sc", SalesContract::getId);
        Page<SalesContractVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<SalesContractVo> records = page.getRecords();
        //赋值客户名称
        customerService.attributeAssign(records, SalesContractVo::getCustomerId, (item, customer) -> {
            item.setCustomerName(customer.getName());
        });
        return page;
    }

    @Override
    public SalesContractVo detail(Long id) {
        SalesContract SalesContract = this.getById(id);
        SalesContractVo result = BeanUtil.toBean(SalesContract, SalesContractVo.class);
        List<SalesContractDetails> salesContractDetailsList = salesContractDetailsService.list(q -> q.eq(SalesContractDetails::getSalesContractId, result.getId()));
        List<SalesContractDetailsVo> salesContractDetailsVos = BeanUtil.copyToList(salesContractDetailsList, SalesContractDetailsVo.class);
        //赋值产品信息
        productInfoService.attributeAssign(salesContractDetailsVos, SalesContractDetailsVo::getProductId, (item, productInfo) -> {
            item.setProductName(productInfo.getName());
        });
        result.setContractDetailsList(salesContractDetailsVos);
        //赋值客户名称
        customerService.attributeAssign(Arrays.asList(result), SalesContractVo::getCustomerId, (item, customer) -> {
            item.setCustomerName(customer.getName());
        });
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SalesContractDto salesContractDto) {
        List<SalesContractDetails> salesContractDetailsList = salesContractDto.getSalesContractDetailsList();
        BigDecimal count = BigDecimal.ZERO;
        for (SalesContractDetails salesContractDetails : salesContractDetailsList) {
            count.add(salesContractDetails.getUnitPrice().multiply(salesContractDetails.getQuantity()));
        }
        salesContractDto.setContractAmount(count);
        salesContractDto.setCode(CodeEnum.SALES_CONTRACT.getCode());
        this.save(salesContractDto);
        for (SalesContractDetails salesContractDetails : salesContractDetailsList) {
            salesContractDetails.setSalesContractId(salesContractDto.getId());
        }
        salesContractDetailsService.saveBatch(salesContractDetailsList);
    }

}
