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

import cn.com.ctop.common.module.entity.Project;
import cn.com.ctop.common.module.service.IAgentConfigService;
import cn.com.ctop.common.module.service.IProductService;
import cn.com.ctop.common.module.service.ISupplierListService;
import cn.com.ctop.common.module.service.ISysUserService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouReportDailyAccountMapper;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.ctop.entity.TransferAccount;
import org.jeecg.modules.ctop.mapper.TransferAccountMapper;
import org.jeecg.modules.ctop.service.IAdvertiserService;
import org.jeecg.modules.ctop.service.ITransferAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 转户记录表
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2020-03-26
 */
@Service
public class TransferAccountServiceImpl extends ServiceImpl<TransferAccountMapper, TransferAccount> implements ITransferAccountService {

    @Autowired
    private KuaishouReportDailyAccountMapper dailyAccountMapper;
    @Autowired
    private TransferAccountMapper transferAccountMapper;

    @Autowired
    private ISysUserService userService;
    @Autowired
    private IAdvertiserService advertiserService;
    @Autowired
    private IProductService productService;
    @Autowired
    private ISupplierListService supplierListService;
    @Autowired
    private IAgentConfigService agentConfigService;

    @Override
    public List<JSONObject> selectCost(Long accountId, String startDate, String endDate) {
        return dailyAccountMapper.selectCost(accountId, startDate, endDate);
    }


    @Override
    public List<TransferAccount> getAccountQuarterDetail(Long accountId, String startDate, String endDate) {
        return transferAccountMapper.getAccountQuarterDetail(accountId, startDate, endDate);
    }


    @Override
    public List<Project> getTransferInfoListByList(List<Project> records) {
        if (Check.isNull(records)) {
            return records;
        }
        Map<String, String> supplierMap = supplierListService.getSupplierMap();
        Map<Long, Long> projectIdMap = new HashMap<>();
        Map<String, String> userIdMap = new HashMap<>();
        Map<String, String> advertiserIdMap = new HashMap<>();
        Map<Long, Long> productIdMap = new HashMap<>();
        for (Project project : records) {
            projectIdMap.put(project.getId(), project.getId());
            if (!Check.isNull(project.getResponsibleId())) {
                userIdMap.put(project.getResponsibleId(), project.getResponsibleId());
            }
            if (!Check.isNull(project.getDesignResponsibleId())) {
                userIdMap.put(project.getDesignResponsibleId(), project.getDesignResponsibleId());
            }
            if (!Check.isNull(project.getSaleId())) {
                userIdMap.put(project.getSaleId(), project.getSaleId());
            }
            if (!Check.isNull(project.getAdvertiserId())) {
                advertiserIdMap.put(project.getAdvertiserId(), project.getAdvertiserId());
            }
            if (!Check.isNull(project.getProductId())) {
                productIdMap.put(project.getProductId(), project.getProductId());
            }
        }
        Map<String, String> userMap = new HashMap<>();
        if (!Check.isNullMap(userIdMap)) {
            userMap = userService.getUserMapByMap(userIdMap);
        }

        Map<String, String> advertiserMap = new HashMap<>();
        if (!Check.isNullMap(advertiserIdMap)) {
            List<JSONObject> advertiserList = advertiserService.getAdvertiserByMap(advertiserIdMap);
            if (!Check.isNull(advertiserList)) {
                for (int i = 0; i < advertiserList.size(); i++) {
                    JSONObject advertiserJson = advertiserList.get(i);
                    advertiserMap.put(advertiserJson.getString("id"), advertiserJson.getString("name"));
                }
            }
        }
        Map<Long, String> productMap = new HashMap<>();
        if (!Check.isNullMap(productIdMap)) {
            List<JSONObject> productList = productService.getProductByMap(productIdMap);
            if (!Check.isNull(productList)) {
                for (int i = 0; i < productList.size(); i++) {
                    JSONObject productJson = productList.get(i);
                    productMap.put(productJson.getLong("id"), productJson.getString("productName"));
                }
            }
        }
        Map<Long, Integer> transferMap = new HashMap<>();
        if (!Check.isNullMap(projectIdMap)) {
            List<JSONObject> transferList = transferAccountMapper.getListByMap(projectIdMap);
            if (!Check.isNull(transferList)) {
                for (int i = 0; i < transferList.size(); i++) {
                    JSONObject transferJson = transferList.get(i);
                    transferMap.put(transferJson.getLong("projectId"), transferJson.getInteger("count"));
                }
            }
        }


        Map<Long, String> agentMap = agentConfigService.getAgentMap();

        for (Project project : records) {
            if (!Check.isNullMap(advertiserMap)) {
                project.setAdvertiserId_dictText(advertiserMap.get(project.getAdvertiserId()));
            }
            if (!Check.isNullMap(userMap)) {
                project.setResponsibleId_dictText(userMap.get(project.getResponsibleId()));
                project.setDesignResponsibleId_dictText(userMap.get(project.getDesignResponsibleId()));
                project.setSaleId_dictText(userMap.get(project.getSaleId()));
            }
            if (!Check.isNullMap(productMap)) {
                project.setProductId_dictText(productMap.get(project.getProductId()));
            }
            if (!Check.isNullMap(transferMap)) {
                project.setNeedExamine(transferMap.get(project.getId()));
            }
            if (!Check.isNullMap(supplierMap)) {
                project.setSupplierCode_dictText(supplierMap.get(project.getSupplierCode()));
            }
            if (!Check.isNull(project.getOcpxActionType()) && !project.getOcpxActionType().isEmpty()) {
                project.setOcpxActionTypeArray(JSONArray.parseArray(project.getOcpxActionType()));
            }
            if (!Check.isNull(project.getDeepConversionType()) && !project.getDeepConversionType().isEmpty()) {
                project.setDeepConversionTypeArray(JSONArray.parseArray(project.getDeepConversionType()));
            }
            if (!Check.isNullMap(agentMap)) {
                if (!Check.isNull(project.getAgentCode())) {
                    project.setAgentCode_dictText(agentMap.get(project.getAgentCode()));
                }

            }
        }
        return records;
    }

    @Override
    public Integer checkDate(Long accountId, String transferorDate) {
        return transferAccountMapper.checkDate(accountId,transferorDate);
    }

}
