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

import cn.com.ctop.common.module.entity.MaterialInfo;
import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.service.IMaterialInfoService;
import cn.com.ctop.common.module.service.IUserAllocationService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouVideoGet;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouVideoGetService;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouReportDailyCreativeMapper;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.ctop.entity.KuaiShouDefectMaterial;
import org.jeecg.modules.ctop.mapper.KuaiShouDefectMaterialMapper;
import org.jeecg.modules.ctop.service.IKuaiShouDefectMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 快手缺失素材
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2020-04-15
 */
@Slf4j
@Service
public class KuaiShouDefectMaterialServiceImpl extends ServiceImpl<KuaiShouDefectMaterialMapper, KuaiShouDefectMaterial> implements IKuaiShouDefectMaterialService {
    @Autowired
    private KuaishouReportDailyCreativeMapper creativeMapper;
    @Autowired
    private IMaterialInfoService materialInfoService;
    @Autowired
    private IKuaiShouVideoGetService videoGetService;
    @Autowired
    private IUserAllocationService userAllocationService;


    @Override
    public void getDefectVideo(String startDate, String endDate) {
        List<JSONObject> list = creativeMapper.selectCodeByDate(startDate, endDate);
        if (!Check.isNull(list)) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject codeJson = list.get(i);
                // charge signature
                String signature = codeJson.getString("signature");
                if (!Check.isNull(signature)) {
                    QueryWrapper<MaterialInfo> materialInfoQueryWrapper = new QueryWrapper<>();
                    materialInfoQueryWrapper.eq("code", signature);
                    materialInfoQueryWrapper.last("limit 1");
                    MaterialInfo materialInfo = materialInfoService.getOne(materialInfoQueryWrapper);
                    if (!Check.isNull(materialInfo)) {
                        continue;
                    }
                    KuaiShouDefectMaterial defectMaterial = new KuaiShouDefectMaterial();
                    defectMaterial.setCharge(codeJson.getBigDecimal("charge"));
                    defectMaterial.setAccountIds(codeJson.getString("accountId"));
                    defectMaterial.setSignature(signature);
                    defectMaterial.setStartDate(startDate);
                    defectMaterial.setEndDate(endDate);
                    QueryWrapper<KuaiShouVideoGet> videoGetQueryWrapper = new QueryWrapper<>();
                    videoGetQueryWrapper.eq("signature", signature);
                    videoGetQueryWrapper.last("limit 1");
                    KuaiShouVideoGet videoGet = videoGetService.getOne(videoGetQueryWrapper);
                    if (Check.isNull(videoGet)) {
                        continue;
                    }
                    defectMaterial.setUrl(videoGet.getUrl());
                    UserAllocation userAllocation = userAllocationService.getByAccountId(videoGet.getAccountId());
                    if (!Check.isNull(userAllocation)) {
                        defectMaterial.setProjectId(userAllocation.getProjectId());
                    }
                    this.save(defectMaterial);
                }
            }
        }
    }
}
