/**
 */
package com.sifang.modules.customer.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.Service.CustomerEntryRentService;
import com.sifang.modules.setting.service.MemberService;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmTrader;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmTraderEmploye;
import com.sifang.modules.sunshine.SystemSetting.service.ProjectCompanyService;
import com.sifang.modules.sunshine.SystemSetting.service.TraderService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sunshine.partnerManagement.entity.CrmPartner;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
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 com.sifang.modules.sys.web.BaseExceptionController;;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.customer.entity.CustomerEntryRent;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

/**
 * 租金信息Controller
 * @author lzp
 * @version 
 */
@Controller
@RequestMapping(value = "${adminPath}/customer/customerEntryRent")
public class CustomerEntryRentController extends BaseExceptionController  {

	@Autowired
	private CustomerEntryRentService customerEntryRentService;


	@Autowired
	MemberService memberService;

	@Autowired
	TraderService traderService;

	@Autowired
	private ProjectCompanyService projectCompanyService;
	@ModelAttribute
	public CustomerEntryRent get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return customerEntryRentService.get(id);
		}else{
			return new CustomerEntryRent();
		}
	}
	@RequestMapping("/entryRentList")
	public String entryRentList(Model model) {

		User user = UserUtils.getUser();
		//代理商列表
		List<Record> traderList = traderService.listTraderRecords(null, new HashMap<String, String>());
		model.addAttribute("traderList", traderList);
		String traderId="";
		if("2".equals(user.getTraderType())){
			traderId = user.getTopTraderId();
		}else if(User.USER_TYPE_ADVISOR.equals(user.getUserType())){
			traderId = CrmUtils.getUserRelId(user);
		}else if(User.USER_TYPE_KJ.equals(user.getUserType())){
			CrmTrader trader = traderService.getTraderBuserId(user.getId());
			traderId=trader.getId();

		}
		List<Record> list=new ArrayList<>();
		if(StringUtil.isBlank(traderId)){
			list=memberService.getProjectLists();
		}else{
			list=memberService.getProjectByTraderLists(traderId);
		}
		model.addAttribute("projectList",list);

		List company=projectCompanyService.getProjectCompany();
		model.addAttribute("company",company);
		return "modules/yangguang/customer/entry/entryRentList";
	}

	@ResponseBody
	@RequestMapping("/ajaxEntryRentList")
	public Object ajaxEntryRentList(HttpServletRequest request){
		Date now = new Date();
		String sn = DateUtils.formatDate(now, "yyyyMMddHHmmss");
		long s1 = System.currentTimeMillis();
		LayuiPage<Record> page = new LayuiPage<>(request);
		Map<String, String> map = getParams(request);
		User user = UserUtils.getUser();

		Operator operator = Operator.of(user.getId(), CrmUtils.getUserXM(user),user.getUserType());
		customerEntryRentService.findEntryRentList(page, map, operator);
		long s2 = System.currentTimeMillis();

		return page.renderJson();
	}

	@RequestMapping("/entryRentListHis")
	public String entryRentListHis(Model model,HttpServletRequest request) {
		String entryId = request.getParameter("entryId");
		model.addAttribute("entryId", entryId);
		return "modules/yangguang/customer/entry/entryRentListHis";
	}

	@ResponseBody
	@RequestMapping("/ajaxEntryRentHis")
	public Object ajaxEntryRentHis(HttpServletRequest request){

		LayuiPage<Record> page = new LayuiPage<>(request);
		String entryId = request.getParameter("entryId");
		customerEntryRentService.findEntryRentHis(page, entryId);
		return page.renderJson();
	}

	/**
	 * 导入弹框
	 */
	@RequestMapping(value = "importCustomerRent")
	public String importCustomerRent(){
		return "modules/yangguang/customer/entry/importCustomerRent";
	}



	@ResponseBody
	@RequestMapping(value = "checkImportCustomerRent")
	public Object checkImportCustomerRent(MultipartFile file) throws IOException {
		Map<String, String> params = Maps.newHashMap();
		params.put("extName", StringUtil.getFileExtName(file.getOriginalFilename()));
		Map<String, Object> retMap = customerEntryRentService.checkImportPowerStationCode(params, file.getInputStream());
		return JSONObject.fromObject(retMap);
	}

	@RequestMapping(value = "importCustomerEntryRent")
	@ResponseBody
	public JSONObject importCustomerEntryRent(MultipartFile file) throws IOException {
		Map<String, String> params = Maps.newHashMap();
		params.put("fileName", file.getOriginalFilename());
		Map<String, Object> retMap = customerEntryRentService.importPowerStationCode(params, file.getInputStream());
		return JSONObject.fromObject(retMap);
	}
}
