package cn.com.countrygarden.bi.main.service.performance.impl.indicators;

import cn.com.countrygarden.bi.main.model.performance.PerformanceArea;
import cn.com.countrygarden.bi.main.model.performance.PerformanceRecord;
import cn.com.countrygarden.bi.main.model.performance.ProfitScale;
import cn.com.countrygarden.bi.main.model.performance.dto.PerformanceShowDataDto;
import cn.com.countrygarden.bi.main.service.performance.indicators.AbstractIndicatorService;
import cn.com.countrygarden.bi.main.util.erp.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 利润规模业务实现类
 *
 * @Author:Wyman
 * @Date:2018/11/3
 */
@Service
public class ProfitScaleServiceImpl<T> extends AbstractIndicatorService<T> {

    @Override
    public Object showData(long elementId, int year, int quarter, int pageCurr, int pageSize, String typeNo) {

        //展示的dto实体
        PerformanceShowDataDto result = this.setVacancyInfo(elementId,year,quarter,typeNo);

        //使用mybatis PageHelper类进行分页
        PageHelper.startPage(pageCurr,pageSize);
        List<PerformanceArea> organList = this.performanceAreaService.getAllAreasByType(typeNo);//查出来的分页记录数

        if(organList.size() != 0){

            //组装返回的数据
            List<ProfitScale> returnList = new ArrayList<>();

            PerformanceRecord record;

            for(PerformanceArea p : organList){

                String hierachyId = p.getHierachyId();
                ProfitScale profitScale=new ProfitScale();
                profitScale.setHierarchyId(hierachyId);//组织id
                profitScale.setSecond_organization(p.getHierachyName());//二级组织
                profitScale.setThird_organization(p.getHierachyName());//三级组织
                profitScale.setYear(year);//设置年份
                profitScale.setQuarter(String.valueOf(quarter));//设置季度

                record = performanceRecordService.getRecordSelective(elementId,year,quarter,hierachyId);
                if(record != null){
                    //设置利润规模
                    try {
                        profitScale.setProScale(String.valueOf(record.getScope()));
                    } catch (Exception e) {
                        profitScale.setProScale("0");
                    }
                }else{
                    profitScale.setProScale("0");
                }
                returnList.add(profitScale);//装载数据
            }
            result.setDataList(returnList);
        }
        return result;
    }

    @Override
    public Object saveData(long elementId, int year, int quarter, List<Object> values) {

        String jsonString = JSONObject.toJSONString(values);

        List<ProfitScale> list = JSON.parseObject(jsonString,new TypeReference<List<ProfitScale>>(){});

        for(ProfitScale profitScale : list){

            String hierachyId = profitScale.getHierarchyId();
            PerformanceRecord record = performanceRecordService.getRecordSelective(elementId,year,quarter,hierachyId);
            long recordId;

            if(record == null){
                record = new PerformanceRecord(hierachyId,elementId,year,quarter);
                record.setScope(StringUtil.isNotEmpty(profitScale.getProScale())? profitScale.getProScale(): "0");
                performanceRecordService.insertPerformanceRecord(record);
            }else{
                recordId = record.getRecordId();
                performanceRecordService.updateScope(StringUtil.isNotEmpty(profitScale.getProScale())? profitScale.getProScale(): "0",recordId);
            }

        }

        PerformanceShowDataDto dataDto = new PerformanceShowDataDto();
        dataDto.setDataList(values);
        //空缺值&空缺地区名称
        List<String> vacanHierachyList = performanceRecordService.getVacancyAreas(elementId,year,quarter,"1");
        dataDto.setNameList(vacanHierachyList);
        int vacancy = vacanHierachyList.size();
        dataDto.setVacancy(vacancy);
        return dataDto;
    }

    @Override
    public void uploadData(long elementId, int year, int quarter, MultipartFile mf, String OperatorId) {

        CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile)mf;
        String fileName = commonsMultipartFile.getOriginalFilename();
        if(!fileName.contains("利润规模")){
            throw new IllegalArgumentException("文件名不正确!");
        }

        //保存数据
        ea.profitScaleControllerUpload(mf, year, quarter, OperatorId);
    }

    @Override
    public Object selectAuthority(long elementId, Map<String, Object> requestMap, HttpServletRequest request) {
        return this.performancePower.getUserPower(String.valueOf(elementId),requestMap,request);
    }
}
