package org.jeecg.modules.orderplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.orderplatform.constants.MaterialType;
import org.jeecg.modules.orderplatform.entity.PlatformMaterial;
import org.jeecg.modules.orderplatform.entity.PlatformOrder;
import org.jeecg.modules.orderplatform.mapper.PlatformOrderMapper;
import org.jeecg.modules.orderplatform.service.IPlatformMaterialService;
import org.jeecg.modules.orderplatform.service.IPlatformOrderService;
import org.jeecg.modules.orderplatform.util.ApprovalNodeNoticeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

import static org.jeecg.modules.orderplatform.util.CodeBuilder.generateCode;

@Service
public class PlatformOrderServiceImpl extends ServiceImpl<PlatformOrderMapper, PlatformOrder> implements IPlatformOrderService {

    @Autowired
    IPlatformMaterialService platFormMaterialService;

    @Autowired
    PlatformOrderMapper platformOrderMapper;

    @Autowired
    ApprovalNodeNoticeHandler approvalNodeNoticeHandler;

    @Override
    public boolean generateMaterials(PlatformOrder platformOrder) {
        boolean result=false;
        //全部为真人视频
        if(MaterialType.REAL.getKey()==platformOrder.getMaterialType()){
            result= generateRealMaterial(platformOrder,platformOrder.getMaterialAmount());
        }
        //全部为剪辑视频
        else if(MaterialType.CUT.getKey()==platformOrder.getMaterialType()){
            result= generateCutMaterial(platformOrder,platformOrder.getMaterialAmount());
        }
        //组合视频
        else if(MaterialType.GROUP.getKey()==platformOrder.getMaterialType()){
            boolean real=generateRealMaterial(platformOrder,platformOrder.getRealAmount());
            boolean cut=generateCutMaterial(platformOrder,platformOrder.getCutAmount());
            result= real&&cut;
        }
        return result;
    }

    @Override
    public boolean updateOrderFinishAmount(String code) {
        QueryWrapper<PlatformOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_code",code);
        PlatformOrder platformOrder= this.getOne(queryWrapper);
        platformOrder.setMaterialFinishAmount(platformOrder.getMaterialFinishAmount()+1);
        platformOrder.setMaterialFinishTime(DateUtils.getDate());
        if(platformOrder.getMaterialAmount().equals(platformOrder.getMaterialFinishAmount())){
            platformOrder.setOrderStatus(3);
            platformOrder.setOrderFinishTime(DateUtils.getDate());
            approvalNodeNoticeHandler.NOTICE_WE_CHAT_ORDER("ORDER_END",code);
        }
        return this.updateById(platformOrder);
    }

    @Override
    public PlatformOrder queryByCode(String code) {
        return platformOrderMapper.queryByCode(code);
    }

    /**
     * 生成真人物料
     */
    private boolean generateRealMaterial(PlatformOrder platformOrder,int amount){
        String orderCode=platformOrder.getOrderCode();
        if(orderCode.isEmpty()){
            return false;
        }
        Long projectId=platformOrder.getProject();
        String orderTitle=platformOrder.getOrderTitle();
        PlatformMaterial platformMaterial;
        List<PlatformMaterial> batch=new ArrayList<>();
        for(int i=0; i<amount ;i++){
            platformMaterial=new PlatformMaterial();
            platformMaterial.setOrderCode(orderCode);
            platformMaterial.setProjectId(projectId);
            platformMaterial.setMaterialType(MaterialType.REAL.getKey());
            platformMaterial.setMaterialStatus(0);
            platformMaterial.setScriptStatus(-1);
            platformMaterial.setVideoStatus(-1);
            //注入唯一标识物料code
            platformMaterial.setMaterialCode(generateCode());
            platformMaterial.setMaterialName(orderTitle+"-"+MaterialType.REAL.getValue()+(i+1));
            batch.add(platformMaterial);
        }
        return platFormMaterialService.saveBatch(batch);
    }

    /**
     * 生成真人物料
     */
    private boolean generateCutMaterial(PlatformOrder platformOrder,int amount){
        String orderCode=platformOrder.getOrderCode();
        if(orderCode.isEmpty()){
            return false;
        }
        Long projectId=platformOrder.getProject();
        String orderTitle=platformOrder.getOrderTitle();
        PlatformMaterial platformMaterial;
        List<PlatformMaterial> batch=new ArrayList<>();
        for(int i=0; i<amount ;i++){
            platformMaterial=new PlatformMaterial();
            platformMaterial.setOrderCode(orderCode);
            platformMaterial.setProjectId(projectId);
            platformMaterial.setMaterialType(MaterialType.CUT.getKey());
            platformMaterial.setMaterialStatus(0);
            platformMaterial.setScriptStatus(-1);
            platformMaterial.setVideoStatus(-1);
            //注入唯一标识物料code
            platformMaterial.setMaterialCode(generateCode());
            platformMaterial.setMaterialName(orderTitle+"-"+MaterialType.CUT.getValue()+(i+1));
            batch.add(platformMaterial);
        }
        return platFormMaterialService.saveBatch(batch);
    }
} 
