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

import cn.com.ctop.common.module.utils.CtopAdConstant;
import cn.com.ctop.common.module.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.ctop.mapper.MaterialTopMapper;
import org.jeecg.modules.ctop.service.IMaterialTopService;
import org.jeecg.modules.ctop.vo.MaterialTopVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  Created by JQ.bi on 2020.05.22
 */
@Slf4j
@Service
public class MaterialTopServiceImpl  implements IMaterialTopService {

    @Autowired
    private MaterialTopMapper materialTopMapper;

    @Override
    public IPage<MaterialTopVO> selectByPage(String endDate, String startDate, String target, String order, int pageNo, int pageSize) {
        QueryWrapper<MaterialTopVO> wrapper = new QueryWrapper<>();
        wrapper.ge("stat_date", startDate).le("stat_date", endDate).ne("url","").isNotNull("url");
        target = StringUtils.humpToLine2(target);
        if(order.equals(CtopAdConstant.COMMON_ORDER_TYPE_ASC)){
            wrapper.orderByAsc(target);
        }else{
            wrapper.orderByDesc(target);
        }
        wrapper.groupBy("code");
        Page<MaterialTopVO> page = new Page<>((pageNo-1)*pageSize,pageSize);
        return materialTopMapper.selectMapsPage(page, wrapper);
    }

    @Override
    public List<MaterialTopVO> selectList(String endDate, String startDate, String target, String order) {
        return materialTopMapper.selectList(endDate,startDate,target,order);
    }

    @Override
    public boolean insertMaterialReportAccount(String statDate) {

        int resultByteDance=materialTopMapper.insertByteDanceMaterialReportAccountByDate(statDate);
        int resultKuaishou=materialTopMapper.insertKuaiShouMaterialReportAccountByDate(statDate);

        if(resultByteDance == 0 || resultKuaishou == 0){
            log.error("整合素材报表信息失败");
            log.info("delete dirty data is start");
            materialTopMapper.deletedMaterialReportAccountByDate(statDate);
            log.info("delete dirty data is success");
        }
        return resultByteDance != 0 && resultKuaishou != 0;
    }

}
