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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.performance.constants.PeriodConstant;
import org.jeecg.modules.performance.constants.RateConstant;
import org.jeecg.modules.performance.entity.DesignDetail;
import org.jeecg.modules.performance.mapper.DesignDetailMapper;
import org.jeecg.modules.performance.service.IDesignDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 *  Created by JQ.bi on 2020.6.22
 */
@Service
@Slf4j
public class DesignDetailServiceImpl extends ServiceImpl<DesignDetailMapper, DesignDetail> implements IDesignDetailService {

    @Autowired
    DesignDetailMapper designDetailMapper;

    @Override
    public void UserCostCalculateByMaterial(String startDate, String endDate, int mediaType) {
        //查询出来素材对应的cost
        List<JSONObject> jsonObjectList = designDetailMapper.queryMaterialDetailByDate(startDate, endDate, mediaType);
        if (jsonObjectList.isEmpty()) {
            log.error("没有素材数据");
        }
        List<DesignDetail> designDetailList = new ArrayList<>();
        jsonObjectList.forEach(it -> {
            String leaderId=it.getString("leaderId")==null?"":it.getString("leaderId");
            //TODO 暂时没有平面
            if (!(null == it.getString("clipId") || it.getString("clipId").isEmpty())) {
                designDetailList.add(this.getDesignDetail(it,"clipId",leaderId,RateConstant.PERCENT_30,startDate,endDate,mediaType));
            }
            if (!(null == it.getString("plan") || it.getString("plan").isEmpty())) {
                designDetailList.add(this.getDesignDetail(it,"plan",leaderId,RateConstant.PERCENT_40,startDate,endDate,mediaType));
            }
            if (!(null == it.getString("shotId") || it.getString("shotId").isEmpty())) {
                designDetailList.add(this.getDesignDetail(it,"clipId",leaderId,RateConstant.PERCENT_10,startDate,endDate,mediaType));
            }
            if (!(null == it.getString("leaderId") || it.getString("leaderId").isEmpty())) {
                designDetailList.add(this.getDesignDetail(it,"leaderId",leaderId,RateConstant.PERCENT_20,startDate,endDate,mediaType));
            }
        });
        this.saveBatch(designDetailList);
    }

    @Override
    public PageInfo<JSONObject> getDesignGroupMaterialByUser(JSONObject requestBody,int pageNo,int pageSize) {
        String userId=requestBody.getString("userId");
        int year= requestBody.getInteger("year");
        String quarter=requestBody.getString("quarter");
        int mediaType=requestBody.getInteger("mediaType");
        String startDate= year+"-"+ PeriodConstant.valueOf(quarter).getStart();
        String endDate= year+"-"+PeriodConstant.valueOf(quarter).getEnd();
        PageHelper.startPage(pageNo,pageSize);
        return new PageInfo(designDetailMapper.queryDesignDetailBy(startDate,endDate,userId,mediaType));
    }

    private DesignDetail getDesignDetail(JSONObject jsonObject,String job, String leaderId,BigDecimal percent,String startDate, String endDate, int mediaType){
        DesignDetail designDetail = new DesignDetail();
        designDetail.setUserId(jsonObject.getString(job));
        designDetail.setMediaType(mediaType);
        designDetail.setMaterialCode(jsonObject.getString("code"));
        designDetail.setMaterialCost(jsonObject.getBigDecimal("cost"));
        designDetail.setPersonCost(jsonObject.getBigDecimal("cost").multiply(percent));
        designDetail.setPercent(percent);
        designDetail.setStartDate(startDate);
        designDetail.setEndDate(endDate);
        designDetail.setLeaderId(leaderId);
        return designDetail;
    }
}
