package ctop.bytedance.ad.advertise.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import ctop.bytedance.ad.advertise.entity.ByteDanceCreativeWordPackage;
import ctop.bytedance.ad.advertise.entity.ByteDanceGeneralCopywriter;
import ctop.bytedance.ad.advertise.mapper.ByteDanceGeneralCopywriterMapper;
import ctop.bytedance.ad.advertise.mapper.BytedanceVideoSlogenInfoMapper;
import ctop.bytedance.ad.advertise.service.IByteDanceGeneralCopywriterService;
import ctop.bytedance.ad.common.service.IUserAllocationService;
import ctop.bytedance.ad.common.utils.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 通用文案库
 * @author jeecg-boot
 * 2021-05-28
 * @version V1.0
 */
@Service
public class ByteDanceGeneralCopywriterServiceImpl extends ServiceImpl<ByteDanceGeneralCopywriterMapper, ByteDanceGeneralCopywriter> implements IByteDanceGeneralCopywriterService {
    @Resource
    ByteDanceGeneralCopywriterMapper byteDanceGeneralCopywriterMapper;
    @Autowired
    IUserAllocationService IUserAllocationService;

    @Override
    public Result getGeneralCopywriterReport(String userId, String accountId, String keyWord, Integer status, String startTime, String endTime, Integer pageNum, Integer pageSize){


        List<String> accountIds = new ArrayList<>();
        //查询普通用户的全量查询，当前用户所有项目
        if(Check.isNull(accountId)){
            List<JSONObject> jsonObjectList = IUserAllocationService.getAccountIdsByUserId(userId);
            if(!Check.isNull(jsonObjectList)){
                for (JSONObject jsonObject:jsonObjectList) {
                    accountIds.add(jsonObject.getString("account_id"));
                }
            }else{
                return Result.successMsg("当前登录人未绑定账户","");
            }

        }else if(accountId == "admin"){//管理员的全量查询
            accountIds = null;
        }else { //不论什么权限的用户的单独搜索一个项目
            accountIds.add(accountId);
        }


        PageHelper.startPage(pageNum,pageSize);
        List<Map<String, Object>> generalCopywriteReportList = byteDanceGeneralCopywriterMapper.selectGeneralCopywriterReport(accountIds, keyWord, status, startTime, endTime);
        if(Check.isNull(generalCopywriteReportList)){
            return Result.successMsg("还没有此文案报表数据，请将此文案投放跑量！",generalCopywriteReportList);
        }

        for (Map<String, Object> generalCopywriteReportMap: generalCopywriteReportList) {

            if(generalCopywriteReportMap.get("count") == null){
                generalCopywriteReportMap.put("count",0);
            }

            BigDecimal cost = new BigDecimal(Check.isNull(generalCopywriteReportMap.get("cost")) ? "0" : generalCopywriteReportMap.get("cost").toString());
            BigDecimal click = new BigDecimal(Check.isNull(generalCopywriteReportMap.get("click")) ? "0" : generalCopywriteReportMap.get("click").toString());
            BigDecimal showNum = new BigDecimal(Check.isNull(generalCopywriteReportMap.get("showNum")) ? "0" : generalCopywriteReportMap.get("showNum").toString());
            BigDecimal convertNum = new BigDecimal(Check.isNull(generalCopywriteReportMap.get("convertNum")) ? "0" : generalCopywriteReportMap.get("convertNum").toString());

            DecimalFormat decimalFormat = new DecimalFormat("0.00#");
            generalCopywriteReportMap.put("cost", decimalFormat.format(cost));
            generalCopywriteReportMap.put("click", click);
            generalCopywriteReportMap.put("showNum", showNum);
            generalCopywriteReportMap.put("convertNum", convertNum);

            //被除数不能为0
            if (StringUtils.equals("0",showNum.toString())){
                generalCopywriteReportMap.put("costPerThousandShow", 0);
                generalCopywriteReportMap.put("clickRate", 0);
            }else {
                BigDecimal costPerThousandShow = cost.multiply(new BigDecimal(1000)).divide(showNum, 2, BigDecimal.ROUND_HALF_UP);
                generalCopywriteReportMap.put("costPerThousandShow", decimalFormat.format(costPerThousandShow));
                BigDecimal clickRate = click.multiply(new BigDecimal(100)).divide(showNum, 2, BigDecimal.ROUND_HALF_UP);
                generalCopywriteReportMap.put("clickRate", decimalFormat.format(clickRate));
            }
            if (StringUtils.equals("0", convertNum.toString())) {
                generalCopywriteReportMap.put("costConvert", 0);
            }else {
                BigDecimal costConvert = cost.divide(convertNum, 2, BigDecimal.ROUND_HALF_UP);
                generalCopywriteReportMap.put("costConvert", decimalFormat.format(costConvert));
            }
            if (StringUtils.equals("0", click.toString())) {
                generalCopywriteReportMap.put("costPerClick", 0);
                generalCopywriteReportMap.put("convertRate", 0);
            }else {
                BigDecimal costPerClick = cost.divide(click, 2, BigDecimal.ROUND_HALF_UP);
                generalCopywriteReportMap.put("costPerClick", decimalFormat.format(costPerClick));
                BigDecimal convertRate = convertNum.multiply(new BigDecimal(100)).divide(click,2,BigDecimal.ROUND_HALF_UP);
                generalCopywriteReportMap.put("convertRate", decimalFormat.format(convertRate));
            }
        }

        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(generalCopywriteReportList);
        return Result.successMsg("数据加载成功。",pageInfo);
    }
    @Override
    public ByteDanceCreativeWordPackage selectCreativeWordPackageByName(String name){
        return byteDanceGeneralCopywriterMapper.selectCreativeWordPackageByName(name);
    }

    @Override
    public List<ByteDanceGeneralCopywriter> selectAllDataByTextCopywriter(String textCopywriter){
        return byteDanceGeneralCopywriterMapper.selectAllDataByTextCopywriter(textCopywriter);
    }

}
