package com.jeeplus.modules.API.caseinfo;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.casebuildinfo.entity.CaseBuildInfo;
import com.jeeplus.modules.casebuildinfo.service.CaseBuildInfoService;
import com.jeeplus.modules.casecategory.entity.CaseCategory;
import com.jeeplus.modules.casecategory.service.CaseCategoryService;
import com.jeeplus.modules.casedesigninfo.entity.CaseDesignInfo;
import com.jeeplus.modules.casedesigninfo.service.CaseDesignInfoService;
import com.jeeplus.modules.caseexecuteinfo.entity.CaseExecuteInfo;
import com.jeeplus.modules.caseexecuteinfo.service.CaseExecuteInfoService;
import com.jeeplus.modules.caseinfo.dao.CaseInfoDao;
import com.jeeplus.modules.caseinfo.entity.CaseInfo;
import com.jeeplus.modules.caseinfo.service.CaseInfoService;
import com.jeeplus.modules.casematerials.entity.CaseMaterials;
import com.jeeplus.modules.casematerials.service.CaseMaterialsService;
import com.jeeplus.modules.casematerialsnorm.entity.CaseMaterialsNorm;
import com.jeeplus.modules.casematerialsnorm.service.CaseMaterialsNormService;
import com.jeeplus.modules.casemechanicalnorm.entity.CaseMechanicalNorm;
import com.jeeplus.modules.casemechanicalnorm.service.CaseMechanicalNormService;
import com.jeeplus.modules.casemember.entity.CaseMember;
import com.jeeplus.modules.casemember.service.CaseMemberService;
import com.jeeplus.modules.casepeoplenorm.entity.CasePeopleNorm;
import com.jeeplus.modules.casepeoplenorm.service.CasePeopleNormService;
import com.jeeplus.modules.sys.entity.Area;
import com.jeeplus.modules.sys.service.AreaService;
import com.jeeplus.modules.utils.ErrorCode;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 案例接口
 * @author
 * @version
 */
@Controller
@RequestMapping(value = "${frontPath}/caseinfo/caseInfo")
public class CaseController extends BaseController {
	@Autowired
	private CaseInfoService caseInfoService;
	@Autowired
	private CaseCategoryService caseCategoryService;
	@Autowired
	private CaseBuildInfoService caseBuildInfoService;
	@Autowired
	private CaseDesignInfoService caseDesignInfoService;
	@Autowired
	private CaseExecuteInfoService caseExecuteInfoService;
	@Autowired
	private CaseMaterialsNormService caseMaterialsNormService;
	@Autowired
	private CaseMechanicalNormService caseMechanicalNormService;
	@Autowired
	private CasePeopleNormService casePeopleNormService;
	@Autowired
	private CaseMaterialsService caseMaterialsService;
	@Autowired
	private CaseMemberService caseMemberService;
	@Autowired
	private AreaService areaService;
	@Autowired
	private CaseInfoDao caseInfoDao;

	/**
	 * 获取案例库列表
	 */
	@ResponseBody
	@RequestMapping(value = "getCaseInfoListMap", method=RequestMethod.GET)
	public AjaxJson caseInfo (HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		try {
			List<Map<String, Object>> mapList = Lists.newArrayList();
			HashMap<String, String> requestMap = findRequestMap(request);
			String cityId = URLDecoder.decode(requestMap.get("cityId") == null ? "" : requestMap.get("cityId"), "UTF-8");
			String name = URLDecoder.decode(requestMap.get("name") == null ? "" : requestMap.get("name"), "UTF-8");
			String classId = URLDecoder.decode(requestMap.get("classId") == null ? "" : requestMap.get("classId"), "UTF-8");
			String dateOrder = URLDecoder.decode(requestMap.get("dateOrder") == null ? "" : requestMap.get("dateOrder"), "UTF-8");
			String memberId = URLDecoder.decode(requestMap.get("memberId") == null ? "" : requestMap.get("memberId"), "UTF-8");
			String cityName = URLDecoder.decode(requestMap.get("cityName") == null ? "" : requestMap.get("cityName"), "UTF-8");// 城市名称
			String proName = URLDecoder.decode(requestMap.get("proName") == null ? "" : requestMap.get("proName"), "UTF-8");// 省级名称
			String page_no = URLDecoder.decode(requestMap.get("page_no") == null ? "" : requestMap.get("page_no"), "UTF-8");
			String page_size = URLDecoder.decode(requestMap.get("page_size") == null ? "" : requestMap.get("page_size"), "UTF-8");
			if (cityId == null || "".equals(cityId) || "(null)".equals(cityId) || "0".equals(cityId)) {
				cityId = null;
				if (proName != null && !"".equals(proName) && !"".equals(cityName) && cityName != null) {
					cityId = areaService.getAreaId(proName, cityName);
				}
			}
			if(StringUtils.isBlank(page_no)){
				page_no="1";
			}
			if(StringUtils.isBlank(page_size)){
				page_size = "10";
			}
			Date startDate = null;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			CaseInfo caseInfo = new CaseInfo();
			if(!"-1".equals(classId)) {
				caseInfo.setCaseCategory(new CaseCategory(classId));
			}
			caseInfo.setStartDate(startDate);
			caseInfo.setArea(new Area(cityId));
			caseInfo.setName(name);
			List<CaseInfo> caseInfoList3 = caseInfoService.findByAllLists(caseInfo);
			j.setErrorCode(ErrorCode.code_1004);//正常数据
			if((caseInfoList3 == null || caseInfoList3.size()== 0) && StringUtils.isBlank(name) && (StringUtils.isBlank(classId) || "-1".equals(classId))){
				caseInfo.setArea(null);
				caseInfoList3 = caseInfoService.findByAllLists(caseInfo);
				j.setErrorCode(ErrorCode.code_3003);//全国数据
			}
			Page<CaseInfo> page = new Page<CaseInfo>();
			page.setPageSize(Integer.parseInt(page_size));
			page.setPageNo(Integer.parseInt(page_no));
			caseInfo.setPage(page);
			caseInfo.setDateOrder(dateOrder);
			List<CaseInfo> caseInfoList = caseInfoService.findByAllLists(caseInfo);
			List<CaseInfo> caseInfoList2 = new ArrayList<CaseInfo>();
			//会员已购买的付费案例
			CaseMember caseMember = new CaseMember();
			caseMember.setMemberId(memberId);
			List<CaseMember> caseMemberList = caseMemberService.findList(caseMember);
			//过滤付费案例
			for(int i = 0; i < caseInfoList.size(); i++){
				CaseInfo c = caseInfoList.get(i);
				if(caseMemberList!=null&&caseMemberList.size()!=0){
					for(int k = 0; k < caseMemberList.size(); k++){
						CaseMember caseMember1 = caseMemberList.get(k);
						if(c.getId().equals(caseMember1.getCaseId())){
							c.setShareStatus("3");
						}
					}
				}
				caseInfoList2.add(c);
			}
			int tp = (caseInfoList3.size() + Integer.parseInt(page_size) -1) / Integer.parseInt(page_size);//总页数
			//获取案例列表
			if (Integer.parseInt(page_no) <= tp && StringUtils.isNotBlank(memberId)) {
				for (int i = 0; i < caseInfoList2.size(); i++) {
					CaseInfo c = caseInfoList2.get(i);
					Map<String, Object> map = Maps.newLinkedHashMap();
					map.put("case_id", c.getId());
					map.put("case_name", StringEscapeUtils.unescapeHtml4(c.getName()));
					if (c.getCaseCategory() != null && !("").equals(c.getCaseCategory())) {
						map.put("case_type", c.getCaseCategory().getName());
						map.put("type_id", c.getCaseCategory().getId());
					}
					if (c.getArea() != null && !("").equals(c.getArea())) {
						map.put("case_address", c.getArea().getName());
						map.put("address_id", c.getArea().getId());
					}
					map.put("case_util", c.getPrice() + c.getUnit());
					if (c.getStartDate() != null) {
						map.put("case_date", c.getStartDate());
					} else {
						map.put("case_date", "");
					}
					map.put("case_pay", c.getShareStatus());
					mapList.add(map);
				}
				if(StringUtils.isBlank(memberId)){
					j.put("data", Lists.newArrayList());
				}else{
					j.put("data", mapList);
				}
			}else{
				j.put("data", new ArrayList<>());
			}
			j.setMsg("获取案例库列表成功");

		} catch (Exception e) {
			logger.error("获取案例库列表失败：", e);
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2004);
			j.setMsg("获取案例库列表失败");
		}
		return j;
	}

	/**
	 * 获取已经购买的案例库列表
	 */
	@ResponseBody
	@RequestMapping(value = "getCaseInfoListBuyMap", method=RequestMethod.GET)
	public AjaxJson caseInfoBuy (HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		try {
			List<Map<String, Object>> mapList = Lists.newArrayList();
			HashMap<String, String> requestMap = findRequestMap(request);
			String cityId = URLDecoder.decode(requestMap.get("cityId") == null ? "" : requestMap.get("cityId"), "UTF-8");
			String name = URLDecoder.decode(requestMap.get("name") == null ? "" : requestMap.get("name"), "UTF-8");
			String classId = URLDecoder.decode(requestMap.get("classId") == null ? "" : requestMap.get("classId"), "UTF-8");
			String dateOrder = URLDecoder.decode(requestMap.get("dateOrder") == null ? "" : requestMap.get("dateOrder"), "UTF-8");
			String memberId = URLDecoder.decode(requestMap.get("memberId") == null ? "" : requestMap.get("memberId"), "UTF-8");
			String cityName = URLDecoder.decode(requestMap.get("cityName") == null ? "" : requestMap.get("cityName"), "UTF-8");// 城市名称
			String proName = URLDecoder.decode(requestMap.get("proName") == null ? "" : requestMap.get("proName"), "UTF-8");// 省级名称
			String page_no = URLDecoder.decode(requestMap.get("page_no") == null ? "" : requestMap.get("page_no"), "UTF-8");
			String page_size = URLDecoder.decode(requestMap.get("page_size") == null ? "" : requestMap.get("page_size"), "UTF-8");
			if (cityId == null || "".equals(cityId) || "(null)".equals(cityId)) {
				if (proName != null && !"".equals(proName) && !"".equals(cityName) && cityName != null) {
					cityId = areaService.getAreaId(proName, cityName);
				}
			}
			if(StringUtils.isBlank(page_no)){
				page_no="1";
			}
			if(StringUtils.isBlank(page_size)){
				page_size = "10";
			}
			Date startDate = null;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			CaseInfo caseInfo = new CaseInfo();
			if (!"-1".equals(classId)) {
				caseInfo.setCaseCategory(new CaseCategory(classId));
			}
			caseInfo.setStartDate(startDate);
			caseInfo.setArea(new Area(cityId));
			caseInfo.setMemberId(memberId);
			caseInfo.setName(name);
			List<CaseInfo> caseInfoList3 = caseInfoDao.findBuyCase(caseInfo);
			Page<CaseInfo> page = new Page<CaseInfo>();
			page.setPageSize(Integer.parseInt(page_size));
			page.setPageNo(Integer.parseInt(page_no));
			caseInfo.setPage(page);
			caseInfo.setDateOrder(dateOrder);
			if(StringUtils.isNotBlank(memberId)){
					Page p = caseInfoService.findBuyPage(page,caseInfo);
					List<CaseInfo> caseInfoList2 = p.getList();
					if(caseInfoList2!=null && caseInfoList2.size()>0){
						for (CaseInfo c:
							 caseInfoList2) {
							c.setShareStatus("3");
						}
					}
				int tp = (caseInfoList3.size() + Integer.parseInt(page_size) -1) / Integer.parseInt(page_size);//总页数
				//获取案例列表
				if (Integer.parseInt(page_no) <= tp && StringUtils.isNotBlank(memberId)) {
					for (int i = 0; i < caseInfoList2.size(); i++) {
						CaseInfo c = caseInfoList2.get(i);
						Map<String, Object> map = Maps.newLinkedHashMap();
						map.put("case_id", c.getId());
						map.put("case_name", StringEscapeUtils.unescapeHtml4(c.getName()));
						if (c.getCaseCategory() != null && !("").equals(c.getCaseCategory())) {
							map.put("case_type", c.getCaseCategory().getName());
							map.put("type_id", c.getCaseCategory().getId());
						}
						if (c.getArea() != null && !("").equals(c.getArea())) {
							map.put("case_address", c.getArea().getName());
							map.put("address_id", c.getArea().getId());
						}
						map.put("case_util", c.getPrice() + c.getUnit());
						if (c.getStartDate() != null) {
							map.put("case_date", c.getStartDate());
						} else {
							map.put("case_date", "");
						}
						map.put("case_pay", c.getShareStatus());
						mapList.add(map);
					}
						j.put("data", mapList);
				}else{
					j.put("data", new ArrayList<>());
				}
			}else{
				j.put("data", new ArrayList<>());
			}
			j.setMsg("获取案例库列表成功");

		} catch (Exception e) {
			logger.error("获取案例库列表失败：", e);
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2004);
			j.setMsg("获取案例库列表失败");
		}
		return j;
	}
	/**
	 * 获取案例库列表
	 */
	@ResponseBody
	@RequestMapping(value = "getCaseCategory", method=RequestMethod.GET)
	public AjaxJson caseCategory (HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		try {
			List<Map<String, Object>> mapList = Lists.newArrayList();
			HashMap<String, String> requestMap = findRequestMap(request);
			String pId = URLDecoder.decode(requestMap.get("classId") == null ? "" : requestMap.get("classId"), "UTF-8");
			CaseCategory caseCategory = new CaseCategory();
			if(StringUtils.isNotBlank(pId)){
				caseCategory.setParent(new CaseCategory(pId));
			}
			List<CaseCategory> caseInfoList = caseCategoryService.findList(caseCategory);
			if(pId == "0"||("0").equals(pId)){
				for (int i = 0; i < caseInfoList.size(); i++) {
					CaseCategory c = caseInfoList.get(i);
					Map<String, Object> map = Maps.newHashMap();
					map.put("category_Id", c.getId());
					map.put("category_Name", c.getName());
					map.put("parent_id", c.getParent().getId());
					mapList.add(map);
				}
			}else{
				for (int i = 0; i < caseInfoList.size(); i++) {
					CaseCategory c = caseInfoList.get(i);
					Map<String, Object> map = Maps.newHashMap();
					map.put("category_Id", c.getId());
					map.put("category_Name", c.getName());
					map.put("parent_id", c.getParent().getId());
					List<Map<String, Object>> mapLists = Lists.newArrayList();
					CaseCategory caseCategory1 = new CaseCategory();
					caseCategory1.setParent(c);
					List<CaseCategory> caseInfoLists = caseCategoryService.findList(caseCategory1);
					for(int k = 0; k < caseInfoLists.size(); k++){
						CaseCategory ck = caseInfoLists.get(k);
						Map<String, Object> maps = Maps.newHashMap();
						maps.put("category_Id", ck.getId());
						maps.put("category_Name", ck.getName());
						maps.put("parent_id", ck.getParent().getId());
						mapLists.add(maps);
					}
					map.put("maplists",mapLists);
					mapList.add(map);
				}
			}
			j.put("data", mapList);
			j.setErrorCode(ErrorCode.code_1004);
			j.setMsg("获取案例分类列表成功");

		} catch (Exception e) {
			logger.error("获取案例分类列表失败：", e);
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2004);
			j.setMsg("获取案例分类列表失败");
		}
		return j;
	}
	/**
	 * 获取详情
	 */
	@ResponseBody
	@RequestMapping(value = "getCaseInfoMap", method=RequestMethod.GET)
	public AjaxJson caseIf(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		try {
			List<Map<String, Object>> mapList1 = Lists.newArrayList();
			List<Map<String, Object>> mapList2 = Lists.newArrayList();
			List<Map<String, Object>> mapList3 = Lists.newArrayList();
			HashMap<String, String> requestMap = findRequestMap(request);
			String caseId = URLDecoder.decode(requestMap.get("caseId") == null ? "" : requestMap.get("caseId"), "UTF-8");
			CaseInfo c = caseInfoService.get(caseId);
			CaseBuildInfo caseBuildInfo = new CaseBuildInfo();// 建设单位子表列表
			CaseDesignInfo caseDesignInfo = new CaseDesignInfo();// 设计单位子表列表
			CaseExecuteInfo caseExecuteInfo = new CaseExecuteInfo();// 施工单位子表列表
			caseBuildInfo.setCaseId(c.getId());
			caseDesignInfo.setCaseId(c.getId());
			caseExecuteInfo.setCaseId(c.getId());
			List<CaseBuildInfo> buyDetailsList1 = caseBuildInfoService.findList(caseBuildInfo);
			List<CaseDesignInfo> buyDetailsList2 = caseDesignInfoService.findList(caseDesignInfo);
			List<CaseExecuteInfo> buyDetailsList3 = caseExecuteInfoService.findList(caseExecuteInfo);
			Map<String, Object> map = Maps.newHashMap();
			map.put("case_name", StringEscapeUtils.unescapeHtml4(c.getName()));
			if(c.getCaseCategory()!=null&&!("").equals(c.getCaseCategory())){
				map.put("case_type", c.getCaseCategory().getName());
				map.put("type_id", c.getCaseCategory().getId());
			}
			if(c.getArea()!=null&&!("").equals(c.getArea())){
				map.put("case_address", c.getArea().getName());
				map.put("address_id", c.getArea().getId());
			}
			map.put("case_util", c.getPrice()+c.getUnit());
			map.put("case_date", c.getStartDate());
			/*map.put("case_from", c.getSource());*/
			if(("1").equals(c.getSource())){
				map.put("case_from", "论文");
			}else if (("2").equals(c.getSource())){
				map.put("case_from", "结算价");
			}
			else if (("3").equals(c.getSource())){
				map.put("case_from", "专家评审");
			}
			else if (("4").equals(c.getSource())){
				map.put("case_from", "其它");
			}
			if(("0").equals(c.getExt())){
				//系统内项目
				if(c.getProject()!=null&&!("").equals(c.getProject())){
					map.put("case_belong", c.getProject().getProjectName());
				}
			}else if(("1").equals(c.getExt())){
				//系统外项目
				map.put("case_belong", c.getContractName());
			}
			for(int i = 0; i < buyDetailsList1.size(); i++){
				CaseBuildInfo c1 = buyDetailsList1.get(i);
				Map<String, Object> map1 = Maps.newHashMap();
				map1.put("utilName", c1.getName());
				map1.put("contact_name", c1.getLinkedman());
				map1.put("email", c1.getEmail());
				map1.put("phone", c1.getMobile());
				mapList1.add(map1);
			}
			for(int i = 0; i < buyDetailsList2.size(); i++){
				CaseDesignInfo c2 = buyDetailsList2.get(i);
				Map<String, Object> map2 = Maps.newHashMap();
				map2.put("utilName", c2.getName());
				map2.put("contact_name", c2.getLinkedman());
				map2.put("email", c2.getEmail());
				map2.put("phone", c2.getMobile());
				mapList2.add(map2);
			}
			for(int i = 0; i < buyDetailsList3.size(); i++){
				CaseExecuteInfo c3 = buyDetailsList3.get(i);
				Map<String, Object> map3 = Maps.newHashMap();
				map3.put("utilName", c3.getName());
				map3.put("contact_name", c3.getLinkedman());
				map3.put("email", c3.getEmail());
				map3.put("phone", c3.getMobile());
				mapList3.add(map3);
			}
			map.put("constructionList", mapList1);
			map.put("participateList", mapList2);
			map.put("operationList", mapList3);
			j.put("data", map);
			j.setErrorCode(ErrorCode.code_1004);
			j.setMsg("获取案例库详情成功");

		} catch (Exception e) {
			logger.error("获取案例库详情失败：", e);
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2004);
			j.setMsg("获取案例库详情失败");
		}
		return j;
	}

	/**
	 * 获取详情
	 */
	@ResponseBody
	@RequestMapping(value = "getCaseDetailMap", method=RequestMethod.GET)
	public AjaxJson caseIfo(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		try {
			HashMap<String, String> requestMap = findRequestMap(request);
			String caseId = URLDecoder.decode(requestMap.get("caseId") == null ? "" : requestMap.get("caseId"), "UTF-8");
			String type = URLDecoder.decode(requestMap.get("type") == null ? "" : requestMap.get("type"), "UTF-8");
			CaseInfo c = caseInfoService.get(caseId);
			Map<String, Object> map = Maps.newHashMap();
			if(("1").equals(type)){
				map.put("detail",c.getPeculiarity());
			}else if(("2").equals(type)){
				map.put("detail",c.getFeature());
			}else if(("3").equals(type)){
				map.put("detail",c.getRecord());
			}
			j.put("data", map);
			j.setErrorCode(ErrorCode.code_1004);
			j.setMsg("获取案例库详情成功");

		} catch (Exception e) {
			logger.error("获取案例库详情失败：", e);
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2004);
			j.setMsg("获取案例库详情失败");
		}
		return j;
	}

	/**
	 * 获取详情
	 */
	@ResponseBody
	@RequestMapping(value = "getCaseInfoNormMap", method=RequestMethod.GET)
	public AjaxJson caseIfNorm(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		try {
			List<Map<String, Object>> mapList = Lists.newArrayList();
			HashMap<String, String> requestMap = findRequestMap(request);
			String caseId = URLDecoder.decode(requestMap.get("caseId") == null ? "" : requestMap.get("caseId"), "UTF-8");
			String type = URLDecoder.decode(requestMap.get("type") == null ? "" : requestMap.get("type"), "UTF-8");
			CaseInfo c = caseInfoService.get(caseId);
			CaseMaterialsNorm caseMaterialsNorm = new CaseMaterialsNorm();  //材料消耗量指标列表
			CaseMechanicalNorm caseMechanicalNorm = new CaseMechanicalNorm();// 	机械消耗量指标列表
			CasePeopleNorm casePeopleNorm = new CasePeopleNorm();   // 人工消耗量指标列表
			caseMaterialsNorm.setCaseId(c.getId());
			caseMechanicalNorm.setCaseId(c.getId());
			casePeopleNorm.setCaseId(c.getId());
			List<CaseMaterialsNorm> buyDetailsList7 = caseMaterialsNormService.findList(caseMaterialsNorm);
			List<CaseMechanicalNorm> buyDetailsList8 = caseMechanicalNormService.findList(caseMechanicalNorm);
			List<CasePeopleNorm> buyDetailsList9 = casePeopleNormService.findList(casePeopleNorm);
			Map<String, Object> map = Maps.newHashMap();
			map.put("case_util", c.getPrice()+c.getUnit());
			if(("1").equals(type)){
				//材料
				for(int i = 0; i < buyDetailsList7.size(); i++){
					CaseMaterialsNorm c7 = buyDetailsList7.get(i);
					Map<String, Object> map7 = Maps.newHashMap();
					map7.put("name",c7.getName());
					map7.put("type",c7.getType());
					map7.put("unitContent",c7.getUnitContent());
					map7.put("unit",c7.getUnit());
					map7.put("allPrice",c7.getAllPrice());
					map7.put("proportion",c7.getProportion());
					mapList.add(map7);
				}
			}else if(("2").equals(type)){
				//机械
				for(int i = 0; i < buyDetailsList8.size(); i++){
					CaseMechanicalNorm c7 = buyDetailsList8.get(i);
					Map<String, Object> map7 = Maps.newHashMap();
					map7.put("name",c7.getName());
					map7.put("type",c7.getType());
					map7.put("unitContent",c7.getUnitContent());
					map7.put("unit",c7.getUnit());
					map7.put("allPrice",c7.getAllPrice());
					map7.put("proportion",c7.getProportion());
					mapList.add(map7);
				}
			}else if(("3").equals(type)){
				//人工
				for(int i = 0; i < buyDetailsList9.size(); i++){
					CasePeopleNorm c7 = buyDetailsList9.get(i);
					Map<String, Object> map7 = Maps.newHashMap();
					map7.put("name",c7.getName());
					map7.put("type",c7.getType());
					map7.put("unitContent",c7.getUnitContent());
					map7.put("unit",c7.getUnit());
					map7.put("allPrice",c7.getAllPrice());
					map7.put("proportion",c7.getProportion());
					mapList.add(map7);
				}
			}
			map.put("indicators_list",mapList);
			j.put("data", map);
			j.setErrorCode(ErrorCode.code_1004);
			j.setMsg("获取案例库指标列表成功");

		} catch (Exception e) {
			logger.error("获取案例库指标列表失败：", e);
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2004);
			j.setMsg("获取案例库指标列表失败");
		}
		return j;
	}
	/**
	 * 获取详情
	 */
	@ResponseBody
	@RequestMapping(value = "getCaseInfoMaterialsMap", method=RequestMethod.GET)
	public AjaxJson caseIfMaterials(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			List<Map<String, Object>> mapList = Lists.newArrayList();
			HashMap<String, String> requestMap = findRequestMap(request);
			String caseId = URLDecoder.decode(requestMap.get("caseId") == null ? "" : requestMap.get("caseId"), "UTF-8");
			String materialName = URLDecoder.decode(requestMap.get("materialName") == null ? "" : requestMap.get("materialName"), "UTF-8");
			CaseInfo c = caseInfoService.get(caseId);
			CaseMaterials caseMaterials = new CaseMaterials();
			caseMaterials.setCaseId(c.getId());
			caseMaterials.setName(materialName);
			List<CaseMaterials> buyDetailsList6 = caseMaterialsService.findList(caseMaterials);
			for (int i = 0; i < buyDetailsList6.size(); i++) {
				CaseMaterials c6 = buyDetailsList6.get(i);
				Map<String, Object> map = Maps.newHashMap();
				map.put("mata_name",c6.getName());
				map.put("mata_model",c6.getSpecification());
				map.put("util",c6.getCalculateUnit());
				map.put("taxprice",c6.getTaxPrice());
				map.put("addition_price",c6.getPrice());
				map.put("Price_date", c6.getUseDate() == null ? "" : sdf.format(c6.getUseDate()));
				map.put("rate",c6.getTax());
				map.put("mata_from",c6.getSource());
				map.put("material",c6.getMaterial());
				map.put("brand",c6.getBrand());
				if(c6.getArea()!=null && !("").equals(c6.getArea())){
					map.put("address",c6.getArea().getName());
				}
				map.put("performance",c6.getPp());
				mapList.add(map);
			}
			j.put("data", mapList);
			j.setErrorCode(ErrorCode.code_1004);
			j.setMsg("获取案例库材价列表成功");

		} catch (Exception e) {
			logger.error("获取案例库材价列表失败：", e);
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2004);
			j.setMsg("获取案例库材价列表失败");
		}
		return j;
	}

	/**
	 * 添加中间表
	 */
	@ResponseBody
	@RequestMapping(value = "memberBuyCase", method=RequestMethod.GET)
	public AjaxJson memberBuyCase(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Object> map = Maps.newHashMap();
        try {
			HashMap<String, String> requestMap = findRequestMap(request);
			String caseId = URLDecoder.decode(requestMap.get("caseId") == null ? "" : requestMap.get("caseId"), "UTF-8");
			String memberId = URLDecoder.decode(requestMap.get("memberId") == null ? "" : requestMap.get("memberId"), "UTF-8");
			if(StringUtils.isBlank(caseId)){
				j.put("status", "false");
				j.setErrorCode(ErrorCode.code_2001);
				j.setMsg("案例数据丢失购买付费案例失败");
			}else if(StringUtils.isBlank(memberId)){
                j.put("status", "false");
				j.setErrorCode(ErrorCode.code_2001);
				j.setMsg("会员数据丢失购买付费案例失败");
			}else{
				CaseMember c = new CaseMember();
				c.setCaseId(caseId);
				c.setMemberId(memberId);
				caseMemberService.save(c);
				j.put("status", "true");
				j.setErrorCode(ErrorCode.code_1004);
				j.setMsg("购买付费案例成功");
			}
		} catch (Exception e) {
			logger.error("购买付费案例失败：", e);
            j.put("status", "false");
			j.setSuccess(false);
			j.setErrorCode(ErrorCode.code_2004);
			j.setMsg("购买付费案例失败");
		}
		return j;
	}
}
