package com.fjhx.sale.service.after.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.common.enums.CodingRuleEnum;
import com.fjhx.common.service.coding.CodingRuleService;
import com.fjhx.common.utils.Assert;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.sale.entity.after.dto.AfterSalesDto;
import com.fjhx.sale.entity.after.dto.AfterSalesSelectDto;
import com.fjhx.sale.entity.after.po.AfterSales;
import com.fjhx.sale.entity.after.vo.AfterSalesVo;
import com.fjhx.sale.entity.contract.po.ContractProduct;
import com.fjhx.sale.mapper.after.AfterSalesMapper;
import com.fjhx.sale.service.after.AfterSalesService;
import com.fjhx.sale.service.contract.ContractProductService;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.common.utils.wrapper.SqlField;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * sale_售后管理 服务实现类
 * </p>
 *
 * @author
 * @since 2024-01-31
 */
@Service
public class AfterSalesServiceImpl extends ServiceImpl<AfterSalesMapper, AfterSales> implements AfterSalesService {

    private final ContractProductService contractProductService;
    private final ProductInfoService productInfoService;
    private final CodingRuleService codingRuleService;

    @Autowired
    public AfterSalesServiceImpl(ContractProductService contractProductService, ProductInfoService productInfoService, CodingRuleService codingRuleService) {
        this.contractProductService = contractProductService;
        this.productInfoService = productInfoService;
        this.codingRuleService = codingRuleService;
    }

    @Override
    public Page<AfterSalesVo> getPage(AfterSalesSelectDto dto) {
        IWrapper<AfterSales> wrapper = getWrapper();

        wrapper.keyword(dto.getKeyword(), new SqlField("as1.code"), new SqlField("c.code"));

        wrapper.orderByDesc("as1", AfterSales::getId);
        Page<AfterSalesVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<AfterSalesVo> records = page.getRecords();
        if (ObjectUtil.isEmpty(records)) {
            return page;
        }

        //赋值产品信息
        productInfoService.attributeAssign(records, AfterSalesVo::getProductId, (detail, product) -> {
            detail.setProductCode(product.getCustomCode());
            detail.setProductName(product.getName());
            detail.setProductLength(product.getLength());
            detail.setProductWidth(product.getWidth());
            detail.setProductHeight(product.getHeight());
        });

        //赋值售后人员名称
        UserUtil.assignmentNickName(records, AfterSalesVo::getUserId, AfterSalesVo::setUserName);

        return page;
    }

    @Override
    public AfterSalesVo detail(Long id) {
        AfterSales AfterSales = this.getById(id);
        AfterSalesVo result = BeanUtil.toBean(AfterSales, AfterSalesVo.class);
        return result;
    }

    @Override
    public void add(AfterSalesDto afterSalesDto) {
        Long contractProductId = afterSalesDto.getContractProductId();
        ContractProduct contractProduct = contractProductService.getById(contractProductId);
        Assert.notEmpty(contractProduct, "查询不到订单产品信息！");

        afterSalesDto.setCode(codingRuleService.createCode(CodingRuleEnum.AFTER_SALES.getKey(), null));

        //赋值信息
        afterSalesDto.setContractId(contractProduct.getContractId());
        afterSalesDto.setProductId(contractProduct.getProductId());

        this.save(afterSalesDto);
    }

    @Override
    public void edit(AfterSalesDto afterSalesDto) {
        this.updateById(afterSalesDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
