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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.area.utils.CustomizeAreaUtil;
import com.fjhx.customer.service.customer.CustomerService;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.victoriatourist.entity.after.dto.AfterSaleRecordDto;
import com.fjhx.victoriatourist.entity.after.dto.AfterSaleRecordSelectDto;
import com.fjhx.victoriatourist.entity.after.po.AfterSaleDetail;
import com.fjhx.victoriatourist.entity.after.po.AfterSaleRecord;
import com.fjhx.victoriatourist.entity.after.vo.AfterSaleDetailVo;
import com.fjhx.victoriatourist.entity.after.vo.AfterSaleRecordVo;
import com.fjhx.victoriatourist.mapper.after.AfterSaleRecordMapper;
import com.fjhx.victoriatourist.service.after.AfterSaleDetailService;
import com.fjhx.victoriatourist.service.after.AfterSaleRecordService;
import com.ruoyi.common.exception.ServiceException;
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-04-19
 */
@Service
public class AfterSaleRecordServiceImpl extends ServiceImpl<AfterSaleRecordMapper, AfterSaleRecord> implements AfterSaleRecordService {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private AfterSaleDetailService afterSaleDetailService;

    @Autowired
    private ProductInfoService productInfoService;

    @Override
    public Page<AfterSaleRecordVo> getPage(AfterSaleRecordSelectDto dto) {
        IWrapper<AfterSaleRecord> wrapper = getWrapper();
        wrapper.eq("asr", AfterSaleRecord::getType, dto.getType());
        wrapper.eq("asr", AfterSaleRecord::getStatus, dto.getStatus());
        wrapper.orderByDesc("asr", AfterSaleRecord::getId);
        Page<AfterSaleRecordVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);

        List<AfterSaleRecordVo> records = page.getRecords();
        if (records.size() == 0) {
            return page;
        }

        // 赋值客户名称
        customerService.attributeAssign(records, AfterSaleRecord::getCustomerId, (item, customer) -> {
            item.setCustomerName(customer.getName());
        });

        // 赋值国省市名称
        CustomizeAreaUtil.setAreaName(records);

        return page;
    }

    @Override
    public AfterSaleRecordVo detail(Long id) {
        AfterSaleRecord AfterSaleRecord = this.getById(id);
        AfterSaleRecordVo result = BeanUtil.toBean(AfterSaleRecord, AfterSaleRecordVo.class);

        List<AfterSaleDetail> list = afterSaleDetailService.list(q -> q.eq(AfterSaleDetail::getAfterSaleRecordId, id));
        List<AfterSaleDetailVo> afterSaleDetailVoList = BeanUtil.copyToList(list, AfterSaleDetailVo.class);
        productInfoService.attributeAssign(afterSaleDetailVoList, AfterSaleDetail::getProductId, (item, product) -> {
            item.setProductCode(product.getCode());
            item.setProductName(product.getName());
            item.setProductSpec(product.getSpec());
        });
        result.setAfterSaleDetailList(afterSaleDetailVoList);
        return result;
    }

    @DSTransactional
    @Override
    public void add(AfterSaleRecordDto afterSaleRecordDto) {
        CustomizeAreaUtil.setAreaId(afterSaleRecordDto);
        this.save(afterSaleRecordDto);

        List<AfterSaleDetail> afterSaleDetailList = afterSaleRecordDto.getAfterSaleDetailList();
        afterSaleDetailList.forEach(item -> item.setAfterSaleRecordId(afterSaleRecordDto.getId()));
        afterSaleDetailService.saveBatch(afterSaleDetailList);
    }

    @DSTransactional
    @Override
    public void edit(AfterSaleRecordDto afterSaleRecordDto) {
        CustomizeAreaUtil.setAreaId(afterSaleRecordDto);
        Long id = afterSaleRecordDto.getId();
        if (ObjectUtil.isEmpty(id)) {
            throw new ServiceException("id不能为空");
        }
        this.updateById(afterSaleRecordDto);

        List<AfterSaleDetail> afterSaleDetailList = afterSaleRecordDto.getAfterSaleDetailList();
        afterSaleDetailList.forEach(item -> item.setAfterSaleRecordId(id));
        afterSaleDetailService.editLinked(afterSaleDetailList, AfterSaleDetail::getAfterSaleRecordId, id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Long id) {
        this.removeById(id);
        afterSaleDetailService.remove(q -> q.eq(AfterSaleDetail::getAfterSaleRecordId, id));
    }

}
