package com.uas.platform.b2b.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.uas.platform.b2b.model.Customer;
import com.uas.platform.b2b.model.User;
import com.uas.platform.b2b.service.CustomerService;
import com.uas.platform.b2b.service.UserService;
import com.uas.platform.core.model.PageInfo;
import com.uas.platform.core.model.PageParams;
import com.uas.platform.core.util.serializer.FlexJsonUtils;
import com.uas.platform.core.web.bind.RequestParams;

@Controller
@RequestMapping("/sale/customers")
public class CustomerController {

	@Autowired
	private CustomerService customerService;

	@Autowired
	private UserService userService;

	/**
	 * 客户资料分页查找
	 * 
	 * @param params
	 * @param authentication
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public Page<Customer> getCustomers(PageParams params, Authentication authentication) {
		User user = userService.findUserByUserUU(Long.parseLong(authentication.getName()));
		return customerService.findAllByPageInfo(user.getEnterprise().getUu(), new PageInfo(params));
	}
	
	/**
	 * 获取客户信息
	 * 
	 * @param custId
	 * @return
	 */
	@RequestMapping(value = "/{custId}", method = RequestMethod.GET)
	@ResponseBody
	public Customer getCustomer(@PathVariable("custId") Long custId) {
		return customerService.findCustomer(custId);
	}

	/**
	 * 保存、修改客户信息
	 * 
	 * @param json
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public List<Customer> saveCustomers(@RequestBody String json, Authentication authentication) {
		List<Customer> customers = FlexJsonUtils.fromJsonArray(json, Customer.class);
		User user = userService.findUserByUserUU(Long.parseLong(authentication.getName()));
		return customerService.saveCustomers(user, customers);
	}

	/**
	 * 审核客户信息
	 * 
	 * @param json
	 * @return
	 */
	@RequestMapping(params = RequestParams.AUDIT, method = RequestMethod.PUT)
	@ResponseBody
	public List<Customer> auditCustomers(@RequestBody String json, Authentication authentication) {
		List<Customer> customers = FlexJsonUtils.fromJsonArray(json, Customer.class);
		User user = userService.findUserByUserUU(Long.parseLong(authentication.getName()));
		return customerService.auditCustomers(user, customers);
	}

	/**
	 * 反审核客户信息
	 * 
	 * @param json
	 * @return
	 */
	@RequestMapping(params = RequestParams.UNAUDIT, method = RequestMethod.PUT)
	@ResponseBody
	public List<Customer> unAuditCustomers(@RequestBody String json, Authentication authentication) {
		List<Customer> customers = FlexJsonUtils.fromJsonArray(json, Customer.class);
		User user = userService.findUserByUserUU(Long.parseLong(authentication.getName()));
		return customerService.unAuditCustomers(user, customers);
	}

	/**
	 * 删除客户信息
	 * 
	 * @param json
	 * @return
	 */
	@RequestMapping(value = "/{custId}", method = RequestMethod.DELETE)
	public ResponseEntity<String> deleteCustomers(@PathVariable("custId") String custIds, Authentication authentication) {
		customerService.deleteCustomers(custIds);
		return new ResponseEntity<String>(HttpStatus.OK);
	}

	/**
	 * 按uu号获取客户信息 <br>
	 * 多个uu号用逗号隔开
	 * 
	 * @param custUU
	 * @return
	 */
	@RequestMapping(value = "/customer/{custUU}", method = RequestMethod.GET)
	@ResponseBody
	public List<Customer> getCustomersByCustUU(@PathVariable("custUU") String custUU,
			Authentication authentication) {
		User user = userService.findUserByUserUU(Long.parseLong(authentication.getName()));
		return customerService.findAllByCustUU(user.getEnterprise().getUu(), custUU);
	}

}
