package com.palmnest.webapp.controller.backstage;

import com.palmnest.model.estateserve.DepositItemInformationForm;
import com.palmnest.service.estateserve.DepositItemInformationFormManager;
import com.palmnest.webapp.controller.BaseFormController;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Controller
@RequestMapping(value = "/service/deposit/*")
public class DepositItemInformationFormController extends BaseFormController {


	@Autowired
	private DepositItemInformationFormManager depositItemInformationFormManager;

	/**
	 * 服务信息押金项配置
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/saveUpdateDepositItemInformationForm", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	public @ResponseBody
	String saveUpdateServiceProject(HttpServletRequest request,
									HttpServletResponse response) throws Exception {
		JSONObject                       jsonObject = new JSONObject();
		List<DepositItemInformationForm> list       = new ArrayList<DepositItemInformationForm>();
		try {
			String myArrays          = request.getParameter("myArrays");
			String delArrays         = request.getParameter("delArrays");
			String serviceTypeInfoId = request.getParameter("serviceTypeInfoId");
			String myArraysArray[]   = myArrays.split(",");
			String delArraysArray[]  = null;
			if (StringUtils.isNotBlank(delArrays)) {
				delArraysArray = delArrays.split(",");
			} else {
				delArraysArray = new String[0];
			}
			DepositItemInformationForm depositItemInformationForm = null;
			for (int i = 0; i < myArraysArray.length; i++) {
				depositItemInformationForm = new DepositItemInformationForm();
				String deposName = request.getParameter("deposName" + myArraysArray[i] + "");
				depositItemInformationForm.setDeposName(deposName);
				String price   = request.getParameter("price" + myArraysArray[i] + "");
				String deposId = request.getParameter("deposId" + myArraysArray[i] + "");
				if (StringUtils.isNotBlank(deposId)) {
					depositItemInformationForm = depositItemInformationFormManager.get(Integer.parseInt(deposId));
					depositItemInformationForm.setDeposName(deposName);
					depositItemInformationForm.setPrice(new BigDecimal(price));
					depositItemInformationForm.setModifier(request.getRemoteUser());
					depositItemInformationForm.setModifyTime(new Date());

				} else {
					depositItemInformationForm.setCreateTime(new Date());
					depositItemInformationForm.setCreator(request.getRemoteUser());
					depositItemInformationForm.setIsEnabled("1");
					depositItemInformationForm.setPrice(new BigDecimal(price));
					depositItemInformationForm.setServiceTypeInfoId(Integer.parseInt(serviceTypeInfoId));
				}
				list.add(depositItemInformationForm);
			}
			for (int i = 0; i < delArraysArray.length; i++) {
				depositItemInformationForm = depositItemInformationFormManager.get(Integer.parseInt(delArraysArray[i]));
				depositItemInformationForm.setIsEnabled("0");
				depositItemInformationForm.setModifier(request.getRemoteUser());
				depositItemInformationForm.setModifyTime(new Date());
				list.add(depositItemInformationForm);
			}
			return JSONObject.fromObject(depositItemInformationFormManager.saveUpdateDepositItemInformationForm(list)).toString();
		} catch (Exception e) {
			log.error("controller saveUpdate saveUpdateServiceProject error{}", e);
			jsonObject.put("flag", false);
			jsonObject.put("desc", "操作失败！");
		}
		return jsonObject.toString();
	}

	/**
	 * 根据服务ID查询押金配置项
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getDepositItemInformationFormList", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	public @ResponseBody
	List<DepositItemInformationForm> getDepositItemInformationFormList(HttpServletRequest request) throws Exception {
		try {
			String                           serviceTypeInfoId = request.getParameter("serviceTypeInfoId");
			List<DepositItemInformationForm> list              = depositItemInformationFormManager.getDepositItemInformationFormList(Integer.parseInt(serviceTypeInfoId));
			return list;
		} catch (Exception e) {
			log.error("controller getDepositItemInformationFormList error {}", e);
		}
		return null;
	}
}
