package pers.lqresier.picc.web.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.lqresier.picc.condition.ProductCondition;
import pers.lqresier.picc.entity.Product;
import pers.lqresier.picc.entity.ProductType;
import pers.lqresier.picc.entity.User;
import pers.lqresier.picc.service.LogService;
import pers.lqresier.picc.service.ProductService;
import pers.lqresier.picc.service.ProductTypeService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
@RequestMapping("/system/product")
@Service
public class ProductController {
private Logger logger = LoggerFactory.getLogger(ProductController.class);
@Autowired
private ProductService productService;
@Autowired
private ProductTypeService productTypeService;
@Autowired
private LogService logService;
/**
* 添加产品
*
* @param name 产品
* @param code 产品编码
* @param documentCode 单证识别码
* @param timeLimit 产品期限
* @param coverage 保额(可以获额多少)
* @param premium 保费
* @param compensat1 赔付1
* @param compensat2 赔付2
* @param compensat3 赔付3
* @param compensat4 赔付4
* @param productType2 险种类型
* @param request
* @param response
* @param session
* @return
*/
@ResponseBody
@RequestMapping("addProduct")
public String addProduct(@RequestParam("product.projectName") String name, @RequestParam("product.projectCode") String code, @RequestParam("product.docuDtg") String documentCode,
@RequestParam("product.productPeriod") String timeLimit, @RequestParam("product.totalCoverage") String coverage, @RequestParam("product.totalPremium") String premium,
@RequestParam("product.duty1") String compensat1, @RequestParam("product.duty2") String compensat2, @RequestParam("product.duty3") String compensat3,
@RequestParam("product.duty4") String compensat4, @RequestParam("product.insuranceId") String productType2, HttpServletRequest request, HttpServletResponse response,
HttpSession session) {
ProductType productType = new ProductType();
if (!"".equals(productType2) && productType2 != null) {
productType.setId(Integer.parseInt(productType2));
}
// 设置相关属性的值
Product product = new Product();
product.setName(name);
product.setCode(code);
product.setDocumentCode(documentCode);
//product.setInfo(info);
if (!"".equals(timeLimit) && timeLimit != null) {
product.setTimeLimit(Integer.parseInt(timeLimit));
}
if (!"".equals(coverage) && coverage != null) {
product.setCoverage(Double.parseDouble(coverage));
}
if (!"".equals(premium) && premium != null) {
product.setPremium(Double.parseDouble(premium));
}
if (!"".equals(compensat1) && compensat1 != null) {
product.setCompensat1(Double.parseDouble(compensat1));
}
if (!"".equals(compensat2) && compensat2 != null) {
product.setCompensat2(Double.parseDouble(compensat2));
}
if (!"".equals(compensat3) && compensat3 != null) {
product.setCompensat3(Double.parseDouble(compensat3));
}
if (!"".equals(compensat4) && compensat4 != null) {
product.setCompensat4(Double.parseDouble(compensat4));
}
product.setType(productType);
// 添加产品
boolean result = false;
try {
result = productService.addProduct(product);
} catch (Exception e) {
result = false;
logger.debug("添加产品失败");
logger.error("添加产品失败");
}
if (result) {
try {
logService.addLog((User) request.getSession().getAttribute("user"), "产品管理", "add", "添加产品[" + product.getName() + "]");
} catch (Exception e) {
logger.debug("添加产品:添加日志失败");
logger.error("添加产品:添加日志失败");
}
return "{\"result\":true,\"tip\":\"添加成功\"}";
} else {
return "{\"result\":false,\"tip\":\"添加失败\"}";
}
}
/**
* 修改产品
*
* @return
*/
@ResponseBody
@RequestMapping("updateProduct")
public String updateProduct(@RequestParam("product.id") int id, @RequestParam("product.projectName") String name, @RequestParam("product.projectCode") String code,
@RequestParam("product.docuDtg") String documentCode, @RequestParam("product.productPeriod") String timeLimit, @RequestParam("product.totalCoverage") String coverage,
@RequestParam("product.totalPremium") String premium, @RequestParam("product.duty1") String compensat1, @RequestParam("product.duty2") String compensat2,
@RequestParam("product.duty3") String compensat3, @RequestParam("product.duty4") String compensat4, @RequestParam("product.insuranceId") String productType2, HttpServletRequest request) {
ProductType productType = new ProductType();
productType.setId(Integer.parseInt(productType2));
// 设置相关属性的值
Product product = new Product();
product.setId(id);
product.setName(name);
product.setCode(code);
product.setDocumentCode(documentCode);
// product.setInfo(info);
if (!"".equals(timeLimit) && timeLimit != null) {
product.setTimeLimit(Integer.parseInt(timeLimit));
}
if (!"".equals(coverage) && coverage != null) {
product.setCoverage(Double.parseDouble(coverage));
}
if (!"".equals(premium) && premium != null) {
product.setPremium(Double.parseDouble(premium));
}
if (!"".equals(compensat1) && compensat1 != null) {
product.setCompensat1(Double.parseDouble(compensat1));
}
if (!"".equals(compensat2) && compensat2 != null) {
product.setCompensat2(Double.parseDouble(compensat2));
}
if (!"".equals(compensat3) && compensat3 != null) {
product.setCompensat3(Double.parseDouble(compensat3));
}
if (!"".equals(compensat4) && compensat4 != null) {
product.setCompensat4(Double.parseDouble(compensat4));
}
product.setType(productType);
// 修改产品
boolean result = false;
try {
result = productService.updateProduct(product);
} catch (Exception e) {
logger.debug("修改产品失败");
logger.error("修改产品失败");
}
if (result) {
try {
logService.addLog((User) request.getSession().getAttribute("user"), "产品管理", "update", "修改产品[" + product.getName() + "]");
} catch (Exception e) {
logger.debug("修改产品:添加日志失败");
logger.error("修改产品:添加日志失败");
}
return "{\"result\":true,\"tip\":\"修改成功\"}";
} else {
return "{\"result\":false,\"tip\":\"修改失败\"}";
}
}