/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.workexample.web;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.exampleeconomics.entity.ExampleEconomics;
import com.jeeplus.modules.exampleeconomics.service.ExampleEconomicsService;
import com.jeeplus.modules.exampleexpend.entity.ExampleExpend;
import com.jeeplus.modules.exampleexpend.service.ExampleExpendService;
import com.jeeplus.modules.exampleproject.entity.ExampleProject;
import com.jeeplus.modules.exampleproject.service.ExampleProjectService;
import com.jeeplus.modules.examplesingle.entity.ExampleSingle;
import com.jeeplus.modules.examplesingle.service.ExampleSingleService;
import com.jeeplus.modules.sys.entity.MainDictDetail;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.service.MainDictDetailService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workexample.entity.WorkExample;
import com.jeeplus.modules.workexample.entity.WorkExampleIndices;
import com.jeeplus.modules.workexample.service.WorkExampleIndicesService;
import com.jeeplus.modules.workexample.service.WorkExampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 案例Controller
 * @author ssrh
 * @version 2018-08-27
 */
@Controller
@RequestMapping(value = "${adminPath}/workexample/workExample")
public class WorkExampleInfoController extends BaseController {

	@Autowired
	private WorkExampleService workExampleService;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private ActTaskService actTaskService;
	@Autowired
	private MainDictDetailService mainDictDetailService;
	@Autowired
	private ExampleSingleService exampleSingleService;
	@Autowired
	private ExampleProjectService exampleProjectService;
	@Autowired
	private ExampleExpendService exampleExpendService;
	@Autowired
	private ExampleEconomicsService exampleEconomicsService;
	@Autowired
	private WorkExampleIndicesService workExampleIndicesService;
	@ModelAttribute
	public WorkExample get(@RequestParam(required=false) String id) {
		WorkExample entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = workExampleService.get(id);
		}
		if (entity == null){
			entity = new WorkExample();
		}
		return entity;
	}

	/**
	 * 查看，增加，编辑指标字典表单页面
	 */
	@RequestMapping(value = "example")
	public String example(WorkExample workExample, Model model) {
        WorkExample example = new WorkExample();
        example.setAddress(workExample.getAddress());
        example.setExampleType(StringUtils.isBlank(workExample.getExampleType())?"1":workExample.getExampleType());
        example.setDictId(workExample.getDictId());
		example.setExampleTypeName(workExample.getExampleTypeName());
		example.setArea(workExample.getArea());
        String exampleType = workExample.getExampleType();
        String type = "0";
        if (StringUtils.isNotBlank(exampleType)){
			WorkExampleIndices workExampleIndices = workExampleIndicesService.get(exampleType);
			if (workExampleIndices!=null && StringUtils.isNotBlank(workExampleIndices.getType())){
				type = workExampleIndices.getType();
			}
		}else {
			example.setExampleTypeName("工程概况");
			example.setExampleType("");
		}
        Office company = UserUtils.getSelectCompany();
        if ("1".equals(type)){
			List<ExampleSingle> exampleSingleList = new ArrayList<>();
			if (StringUtils.isNotBlank(workExample.getDictId())){
				exampleSingleList = exampleSingleService.getCount(company.getId(),workExample.getArea().getId(),workExample.getDictId(),exampleType);
				if (exampleSingleList!=null && exampleSingleList.size()!=0) {
					workExample.setCompanyId(company.getId());
					BigDecimal sum = workExampleService.getSumBuildScale(workExample);
					ExampleSingle exampleSingleTotal=new ExampleSingle();
					exampleSingleTotal.setName("合计");
					BigDecimal sumsTotal = new BigDecimal("0.00");
					for (ExampleSingle exampleSingle : exampleSingleList) {
						sumsTotal = sumsTotal.add(exampleSingle.getSums());
					}
					for (ExampleSingle exampleSingle : exampleSingleList) {
						BigDecimal proportion = exampleSingle.getSums().divide(sumsTotal, 2, BigDecimal.ROUND_HALF_EVEN).multiply(new BigDecimal(100));
						exampleSingle.setProportion(proportion.toString());
						if (exampleSingle.getAvgs() != null && exampleSingle.getAvgs().compareTo(BigDecimal.ZERO) == 1) {
							exampleSingle.setAvgs(exampleSingle.getSums().divide(exampleSingle.getAvgs(), 2, BigDecimal.ROUND_HALF_EVEN));
						}
					}
					exampleSingleTotal.setSums(sumsTotal);
					exampleSingleTotal.setAvgs(sumsTotal.divide(sum, 2, BigDecimal.ROUND_HALF_EVEN));
					exampleSingleList.add(exampleSingleTotal);
				}
            }
			model.addAttribute("workExample", example);
			model.addAttribute("exampleSingleList", exampleSingleList);
            return "modules/workexample/workExampleSingle";
        }else if("4".equals(type)){
			List<ExampleEconomics> exampleEconomicsList = new ArrayList<>();
			if (StringUtils.isNotBlank(workExample.getDictId())){
				exampleEconomicsList = exampleEconomicsService.getCount(company.getId(),workExample.getArea().getId(),workExample.getDictId(),exampleType);
				if (exampleEconomicsList!=null && exampleEconomicsList.size()!=0) {
					workExample.setCompanyId(company.getId());
					BigDecimal sum = workExampleService.getSumBuildScale(workExample);
					ExampleEconomics exampleEconomicsTotal=new ExampleEconomics();
					exampleEconomicsTotal.setName("合计");
					BigDecimal sumsTotal = new BigDecimal("0.00");
					for (ExampleEconomics exampleEconomics : exampleEconomicsList) {
						sumsTotal = sumsTotal.add(exampleEconomics.getSums());
					}
					for (ExampleEconomics exampleEconomics : exampleEconomicsList) {
						BigDecimal proportion = exampleEconomics.getSums().divide(sumsTotal, 2, BigDecimal.ROUND_HALF_EVEN).multiply(new BigDecimal(100));
						exampleEconomics.setProportion(proportion.toString());
						if (exampleEconomics.getAvgs() != null && exampleEconomics.getAvgs().compareTo(BigDecimal.ZERO) == 1) {
							exampleEconomics.setAvgs(exampleEconomics.getSums().divide(exampleEconomics.getAvgs(), 2, BigDecimal.ROUND_HALF_EVEN));
						}
					}
					exampleEconomicsTotal.setSums(sumsTotal);
					exampleEconomicsTotal.setAvgs(sumsTotal.divide(sum, 2, BigDecimal.ROUND_HALF_EVEN));
					exampleEconomicsList.add(exampleEconomicsTotal);
				}
			}
			model.addAttribute("workExample", example);
			model.addAttribute("exampleEconomicsList", exampleEconomicsList);
			return "modules/workexample/workExampleEconomics";
		}else if("5".equals(type)){
			List<ExampleProject> exampleProjectList = new ArrayList<>();
			if (StringUtils.isNotBlank(workExample.getDictId())){
				exampleProjectList = exampleProjectService.getCount(company.getId(),workExample.getArea().getId(),workExample.getDictId(),exampleType);
				if (exampleProjectList!=null && exampleProjectList.size()!=0) {
					workExample.setCompanyId(company.getId());
					for (ExampleProject exampleProject : exampleProjectList) {
						if (exampleProject.getAvgs() != null && exampleProject.getAvgs().compareTo(BigDecimal.ZERO) == 1) {
							BigDecimal proportion = exampleProject.getSums().divide(exampleProject.getAvgs(), 2, BigDecimal.ROUND_HALF_EVEN).multiply(new BigDecimal(100));
							exampleProject.setAvgs(proportion);
						}
					}
				}
			}
			model.addAttribute("workExample", example);
			model.addAttribute("exampleProjectList", exampleProjectList);
			return "modules/workexample/workExampleWork";
		}else if("6".equals(type)){
			List<ExampleExpend> exampleExpendList = new ArrayList<>();
			if (StringUtils.isNotBlank(workExample.getDictId())){
				exampleExpendList = exampleExpendService.getCount(company.getId(),workExample.getArea().getId(),workExample.getDictId(),exampleType);
				if (exampleExpendList!=null && exampleExpendList.size()!=0) {
					workExample.setCompanyId(company.getId());
					for (ExampleExpend exampleExpend : exampleExpendList) {
						if (exampleExpend.getAvgs() != null && exampleExpend.getAvgs().compareTo(BigDecimal.ZERO) == 1) {
							BigDecimal proportion = exampleExpend.getSums().divide(exampleExpend.getAvgs(), 2, BigDecimal.ROUND_HALF_EVEN).multiply(new BigDecimal(100));
							exampleExpend.setAvgs(proportion);
						}
					}
				}
			}
			model.addAttribute("workExample", example);
			model.addAttribute("exampleExpendList", exampleExpendList);
			return "modules/workexample/workExampleConsumption";
		}else {
	    	if (StringUtils.isNotBlank(workExample.getDictId())){
				workExample.setCompanyId(company.getId());
				example = workExampleService.getByTypes(workExample);
				MainDictDetail mainDictDetail = mainDictDetailService.get(workExample.getDictId());
				example.setWorkType(mainDictDetail!=null?mainDictDetail.getDetailName():"");
			}
			example.setExampleTypeName("工程概况");
	    	if(StringUtils.isBlank(example.getDictId())){
				example.setDictId(workExample.getDictId());
			}
			if(workExample.getArea()== null || StringUtils.isNotBlank(workExample.getArea().getId())){
				example.setArea(workExample.getArea());
			}
            model.addAttribute("workExample", example);
            return "modules/workexample/workExampleSituation";
		}
	}

}