package com.fjhx.jxst.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.core.toolkit.IdWorker;
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.file.utils.ObsFileUtil;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.jxst.entity.after.dto.AfterSalesRecordDetailDto;
import com.fjhx.jxst.entity.after.dto.AfterSalesRecordDto;
import com.fjhx.jxst.entity.after.dto.AfterSalesRecordSelectDto;
import com.fjhx.jxst.entity.after.po.AfterSalesRecord;
import com.fjhx.jxst.entity.after.po.AfterSalesRecordDetail;
import com.fjhx.jxst.entity.after.vo.AfterSalesRecordVo;
import com.fjhx.jxst.mapper.after.AfterSalesRecordMapper;
import com.fjhx.jxst.service.after.AfterSalesRecordDetailService;
import com.fjhx.jxst.service.after.AfterSalesRecordService;
import com.fjhx.jxst.utils.CodeEnum;
import com.fjhx.mes.entity.bom.po.BomDetail;
import com.fjhx.mes.entity.bom.po.BomInfo;
import com.fjhx.mes.entity.bom.vo.BomDetailVo;
import com.fjhx.mes.entity.production.po.ProductionTaskDetail;
import com.fjhx.mes.entity.production.vo.ProductionTaskDetailVo;
import com.fjhx.mes.entity.work.po.WorkOrder;
import com.fjhx.mes.entity.work.po.WorkOrderBom;
import com.fjhx.mes.service.bom.BomDetailService;
import com.fjhx.mes.service.bom.BomInfoService;
import com.fjhx.mes.service.production.ProductionTaskDetailService;
import com.fjhx.mes.service.work.WorkOrderBomService;
import com.fjhx.mes.service.work.WorkOrderService;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.common.utils.wrapper.SqlField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 售后记录 服务实现类
 * </p>
 *
 * @author
 * @since 2023-05-25
 */
@Service
public class AfterSalesRecordServiceImpl extends ServiceImpl<AfterSalesRecordMapper, AfterSalesRecord> implements AfterSalesRecordService {

    @Autowired
    private ProductionTaskDetailService productionTaskDetailService;
    @Autowired
    private WorkOrderService workOrderService;
    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private WorkOrderBomService workOrderBomService;
    @Autowired
    private BomInfoService bomInfoService;
    @Autowired
    private BomDetailService bomDetailService;
    @Autowired
    private AfterSalesRecordDetailService afterSalesRecordDetailService;
    @Autowired
    private CodingRuleService codingRuleService;

    @Override
    public Page<AfterSalesRecordVo> getPage(AfterSalesRecordSelectDto dto) {
        IWrapper<AfterSalesRecord> wrapper = getWrapper();

        //关键字搜索
        wrapper.keyword(dto.getKeyword(),
                new SqlField("asr",AfterSalesRecord::getCode),
                new SqlField("asr",AfterSalesRecord::getProductSn),
                new SqlField("asr",AfterSalesRecord::getRemark),
                new SqlField("asr",AfterSalesRecord::getAfterSalesRemark),
                new SqlField("asr",AfterSalesRecord::getAmount)
        );

        wrapper.orderByDesc("asr", AfterSalesRecord::getId);
        Page<AfterSalesRecordVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<AfterSalesRecordVo> records = page.getRecords();
        //赋值产品信息
        productInfoService.attributeAssign(records, AfterSalesRecordVo::getProductId, (item, productInfo) -> {
            item.setProductName(productInfo.getName());
            item.setProductSpec(productInfo.getSpec());
        });
        return page;
    }

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

        //赋值产品信息
        productInfoService.attributeAssign(Arrays.asList(result), AfterSalesRecordVo::getProductId, (item, productInfo) -> {
            item.setProductName(productInfo.getName());
            item.setProductSpec(productInfo.getSpec());
        });

        ProductionTaskDetailVo productionTaskDetailVo = productionTaskDetailService.snInfo(result.getProductSn());
        if (ObjectUtil.isEmpty(productionTaskDetailVo)) {
            return result;
        }
        //获取工单信息
        WorkOrder workOrder = workOrderService.getById(productionTaskDetailVo.getWorkOrderId());
        //查询BOM信息
        BomInfo bomInfo = bomInfoService.getOne(q -> q.eq(BomInfo::getProductId, workOrder.getProductId()).eq(BomInfo::getCurrentVersion, 1));
        Assert.notEmpty(bomInfo, "查询不到产品的BOM信息");
        result.setBomInfoId(bomInfo.getId());

        List<BomDetailVo> bomDetailList = new ArrayList<>();
        //判断是否定制
        if ("1".equals(workOrder.getIsCustomized())) {
            //是定制
            List<WorkOrderBom> list = workOrderBomService.list(q -> q.eq(WorkOrderBom::getWorkOrderId, workOrder.getId()));
            bomDetailList = BeanUtil.copyToList(list, BomDetailVo.class);
        } else {
            //不是定制
            List<BomDetail> list = bomDetailService.list(q -> q.eq(BomDetail::getBomInfoId, bomInfo.getId()));
            bomDetailList = BeanUtil.copyToList(list, BomDetailVo.class);
        }
        Assert.notEmpty(bomDetailList, "查询不到产品的BOM明细");
        //给bom明细赋值产品名称
        productInfoService.attributeAssign(bomDetailList, BomDetailVo::getProductId, (item, productInfo) -> {
            item.setProductName(productInfo.getName());
        });

        //赋值售后明细
        Map<Long, AfterSalesRecordDetail> afterSalesRecordDetailMap = afterSalesRecordDetailService.mapKEntity(AfterSalesRecordDetail::getAccessoriesId,
                q -> q.eq(AfterSalesRecordDetail::getAfterSalesRecordId, id));
        for (BomDetailVo bomDetailVo : bomDetailList) {
            AfterSalesRecordDetail afterSalesRecordDetail = afterSalesRecordDetailMap.get(bomDetailVo.getProductId());
            bomDetailVo.setAfterSalesRecordDetail(afterSalesRecordDetail);
        }

        result.setBomDetailList(bomDetailList);
        return result;
    }

    @DSTransactional
    @Override
    public void add(AfterSalesRecordDto dto) {
        Assert.notEmpty(dto.getProductSn(), "产品sn不能为空");

        //获取产品信息
        ProductionTaskDetail productionTaskDetail = productionTaskDetailService.getOne(q -> q
                .eq(ProductionTaskDetail::getProductSn, dto.getProductSn())
        );
        Assert.notEmpty(productionTaskDetail, "查询不到该sn信息");
        dto.setProductId(productionTaskDetail.getProductId());

        //赋值单号
//        dto.setCode(CodeEnum.AFTER_SALES.getCode());
        dto.setCode(codingRuleService.createCode(CodingRuleEnum.JXST_AFTER_SALES.getKey(),null));

        this.save(dto);

        ObsFileUtil.editFile(dto.getFileList(),dto.getId());
    }

    @DSTransactional
    @Override
    public void afterSales(AfterSalesRecordDto afterSalesRecordDto) {
        Assert.notEmpty(afterSalesRecordDto.getId(), "售后id不能为空");
        Assert.notEmpty(afterSalesRecordDto.getAfterSalesRecordDetailList(), "售后明细列表不能为空");
        afterSalesRecordDto.setStatus(1);//修改为售后完成
        this.updateById(afterSalesRecordDto);
        List<AfterSalesRecordDetailDto> afterSalesRecordDetailList = afterSalesRecordDto.getAfterSalesRecordDetailList();
        for (AfterSalesRecordDetailDto afterSalesRecordDetailDto : afterSalesRecordDetailList) {
            long id = IdWorker.getId();
            afterSalesRecordDetailDto.setId(id);
            afterSalesRecordDetailDto.setAfterSalesRecordId(afterSalesRecordDto.getId());
            ObsFileUtil.saveFile(afterSalesRecordDetailDto.getFileList(), id);
        }
        List<AfterSalesRecordDetail> afterSalesRecordDetails = BeanUtil.copyToList(afterSalesRecordDetailList, AfterSalesRecordDetail.class);
        afterSalesRecordDetailService.saveBatch(afterSalesRecordDetails);
    }

}
