package com.fjhx.sale.service.after.impl;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.utils.Assert;
import com.fjhx.customer.service.customer.CustomerService;
import com.fjhx.item.entity.product.po.ProductInfo;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.sale.entity.after.dto.AfterSalesDetailDto;
import com.fjhx.sale.entity.after.dto.AfterSalesDetailSelectDto;
import com.fjhx.sale.entity.after.po.AfterSalesDetail;
import com.fjhx.sale.entity.after.vo.AfterSalesDetailVo;
import com.fjhx.sale.entity.contract.po.ContractProduct;
import com.fjhx.sale.mapper.after.AfterSalesDetailMapper;
import com.fjhx.sale.service.after.AfterSalesDetailService;
import com.fjhx.sale.service.after.AfterSalesService;
import com.fjhx.sale.service.contract.ContractProductService;
import com.fjhx.sale.service.contract.ContractService;
import com.fjhx.wms.entity.stock.emums.StockWaitType;
import com.fjhx.wms.entity.stock.po.StockWait;
import com.fjhx.wms.entity.stock.po.StockWaitDetails;
import com.fjhx.wms.service.stock.StockWaitDetailsService;
import com.fjhx.wms.service.stock.StockWaitService;
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.util.List;


/**
 * <p>
 * 售后明细 服务实现类
 * </p>
 *
 * @author
 * @since 2023-07-20
 */
@Service
public class AfterSalesDetailServiceImpl extends ServiceImpl<AfterSalesDetailMapper, AfterSalesDetail> implements AfterSalesDetailService {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private ContractService contractService;
    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private StockWaitService stockWaitService;
    @Autowired
    private StockWaitDetailsService stockWaitDetailsService;
    @Autowired
    private ContractProductService contractProductService;
    @Autowired
    private AfterSalesService afterSalesService;

    @Override
    public Page<AfterSalesDetailVo> getPage(AfterSalesDetailSelectDto dto) {
        IWrapper<AfterSalesDetail> wrapper = getWrapper();
        //客户id过滤
        wrapper.eq("as1", AfterSalesDetailVo::getCustomerId, dto.getCustomerId());
        //售后类型过滤
        wrapper.eq("as1", AfterSalesDetailVo::getType, dto.getType());
        //售后状态过滤
        wrapper.eq("asd", AfterSalesDetailVo::getStatus, dto.getStatus());
        //关键字搜索
        if (ObjectUtils.isNotEmpty(dto.getKeyword())) {
            List<Long> pIds = productInfoService.listObject(ProductInfo::getId, q -> q
                    .like(ProductInfo::getCode, dto.getKeyword())
                    .like(ProductInfo::getName, dto.getKeyword())
                    .like(ProductInfo::getSpec, dto.getKeyword())
            );
            pIds.add(null);
            wrapper.in("asd", AfterSalesDetail::getProductId, pIds);
        }
        //根据创建时间倒序
        wrapper.orderByDesc("as1", AfterSalesDetail::getCreateTime);
        Page<AfterSalesDetailVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<AfterSalesDetailVo> records = page.getRecords();
        //赋值客户名称
        customerService.attributeAssign(records, AfterSalesDetailVo::getCustomerId, (item, customer) -> {
            item.setCustomerName(customer.getName());
        });
        //赋值合同编号
        contractService.attributeAssign(records, AfterSalesDetailVo::getContractId, (item, contract) -> {
            item.setContractCode(contract.getCode());
        });
        //赋值产品信息
        productInfoService.attributeAssign(records, AfterSalesDetailVo::getProductId, (item, productInfo) -> {
            item.setProductCode(productInfo.getCode());
            item.setProductName(productInfo.getName());
            item.setProductSpec(productInfo.getSpec());
            item.setProductUnit(productInfo.getUnit());
        });
        return page;
    }

    @Override
    public void edit(AfterSalesDetailDto afterSalesDetailSelectDto) {
        this.updateById(afterSalesDetailSelectDto);
    }

    /**
     * 退货
     */
    @DSTransactional
    @Override
    public void salesReturn(AfterSalesDetailDto afterSalesDetailDto) {
        Assert.notEmpty(afterSalesDetailDto.getId(), "售后id不能为空");
        AfterSalesDetail afterSalesDetail = this.getById(afterSalesDetailDto.getId());
        Assert.notEmpty(afterSalesDetail, "查询不到售后信息");

        //生成售后退货待入库信息
        StockWait stockWait = new StockWait();
        stockWait.setType(1);//入库
        stockWait.setStatus(0);//待入库
        stockWait.setBusinessType(StockWaitType.SALES_RETURN_IN.getDetailType());
        stockWait.setBusinessId(afterSalesDetail.getId());
        stockWaitService.save(stockWait);

        //生成售后退货待入库明细
        StockWaitDetails stockWaitDetails = new StockWaitDetails();
        stockWaitDetails.setStockWaitId(stockWait.getId());
        stockWaitDetails.setProductId(afterSalesDetail.getProductId());
        stockWaitDetails.setQuantity(afterSalesDetail.getQuantity());
        stockWaitDetailsService.save(stockWaitDetails);
    }

    /**
     * 销售换货
     */
    @DSTransactional
    @Override
    public void salesReplace(AfterSalesDetailDto afterSalesDetailDto) {
        Assert.notEmpty(afterSalesDetailDto.getId(), "售后id不能为空");
        AfterSalesDetail afterSalesDetail = this.getById(afterSalesDetailDto.getId());
        Assert.notEmpty(afterSalesDetail, "查询不到售后信息");

        //生成售后换货待入库信息
        StockWait stockWait = new StockWait();
        stockWait.setType(1);//入库
        stockWait.setStatus(0);//待入库
        stockWait.setBusinessType(StockWaitType.SALES_REPLACE_IN.getDetailType());
        stockWait.setBusinessId(afterSalesDetailDto.getId());
        stockWaitService.save(stockWait);
        //生成售后换货待出库信息
        StockWait stockWait1 = new StockWait();
        stockWait1.setType(2);//出库
        stockWait1.setStatus(0);//待出库
        stockWait1.setBusinessType(StockWaitType.SALES_REPLACE_OUT.getDetailType());
        stockWait1.setBusinessId(afterSalesDetailDto.getId());
        stockWaitService.save(stockWait1);

        //生成售后换货待出入库明细
        //生成入库明细
        StockWaitDetails stockWaitDetails = new StockWaitDetails();
        stockWaitDetails.setStockWaitId(stockWait.getId());
        stockWaitDetails.setProductId(afterSalesDetail.getProductId());
        stockWaitDetails.setQuantity(afterSalesDetail.getQuantity());
        stockWaitDetailsService.save(stockWaitDetails);
        //生成出库明细
        StockWaitDetails stockWaitDetails1 = new StockWaitDetails();
        stockWaitDetails1.setStockWaitId(stockWait1.getId());
        stockWaitDetails1.setProductId(afterSalesDetail.getProductId());
        stockWaitDetails1.setQuantity(afterSalesDetail.getQuantity());
        stockWaitDetailsService.save(stockWaitDetails1);
    }

    /**
     * 发起采购
     */
    @Override
    @Transactional
    public void initPurchase(AfterSalesDetailDto afterSalesDetailDto) {
        Assert.notEmpty(afterSalesDetailDto.getId(), "售后id不能为空");
        AfterSalesDetail afterSalesDetail = this.getById(afterSalesDetailDto.getId());
        Assert.notEmpty(afterSalesDetail, "查询不到售后信息");
//        AfterSales afterSales = afterSalesService.getById(afterSalesDetail.getAfterSalesId());

        //创建交接单
        ContractProduct contractProduct = new ContractProduct();
        contractProduct.setProductId(afterSalesDetail.getProductId());
        contractProduct.setQuantity(afterSalesDetail.getQuantity());
        contractProduct.setExpendQuantity(afterSalesDetail.getQuantity());
        contractProduct.setContractId(afterSalesDetailDto.getContractId());
        contractProduct.setPrice(afterSalesDetail.getPrice());
        contractProduct.setAmount(afterSalesDetail.getAmount());
        contractProduct.setRemark(afterSalesDetail.getRemark());
        contractProductService.save(contractProduct);
    }

}
