package lingtao.net.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lingtao.net.bean.CustomerData;
import lingtao.net.bean.Msg;
import lingtao.net.bean.SysUser;
import lingtao.net.service.CustomerDataService;

@RestController
public class CustomerDataControlle {

	@Autowired
	private CustomerDataService customerDataService;

	@RequestMapping("/getCustomerDatas")
	public Msg getCustomerDatas(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit, CustomerData customerData) {
		PageHelper.startPage(page, limit);
		List<CustomerData> customerDatalist = customerDataService.getCustomerDatas(customerData);
		PageInfo<CustomerData> pageInfo = new PageInfo<CustomerData>(customerDatalist);
		return Msg.success().add("list", pageInfo);
	}

	/**
	 * 添加数据
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/addCustomerData")
	public Msg addCustomerData(CustomerData customerData) throws Exception {
		// 把字符串转为日期格式
		customerDataService.addCustomerData(customerData);
		return Msg.success();
	}

	/**
	 * 修改数据
	 */
	@RequestMapping("/updateCustomerData")
	public Msg updateCustomerData(CustomerData customerData) {
		customerDataService.updateCustomerDataById(customerData);
		return Msg.success();
	}

	/**
	 * 删除数据
	 */
	@RequestMapping("/deleteCustomerData")
	public Msg deleteCustomerData(@RequestParam("id") Integer id) {
		customerDataService.deleteCustomerDataById(id);
		return Msg.success();
	}

	/**
	 * 店长修改说明数据
	 * 
	 * @param customerData
	 */
	@RequestMapping("/updateRemarkById")
	public Msg updateRemarkById(@RequestParam(value = "id") int id, @RequestParam(value = "field") String field,
			@RequestParam(value = "value") String value) {
		// 超管、组长身份才允许修改comment
		boolean flag = isSuperOrManager();
		if (!flag) {
			return Msg.fail();
		}
		customerDataService.updateRemarkById(id, field, value);
		return Msg.success();
	}

	/**
	 * 修改完成状态
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/changeIsBuy")
	public Msg changeIsBuy(@RequestParam(value = "id") Integer id, @RequestParam(value = "username") String username) {
		// 超管、组长身份才允许修改【成交状态】
		boolean flag = isSuperOrManager();
		if (!flag) {
			return Msg.fail();
		}
		return customerDataService.changeIsBuy(id);
		// 只有自己创建的数据才能更改【完成状态】
		/*
		 * if (username.equals(user.getUsername())) { }
		 */
		// return Msg.fail();
	}

	// 是否有超管或者组长身份
	public boolean isSuperOrManager() {
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		String role = user.getRole();
		boolean flag = false;
		if (role.contains(",")) {
			String[] split = role.split(",");
			for (int i = 0; i < split.length; i++) {
				if ("1011".equals(split[i]) || "1".equals(split[i])) {
					flag = true;
					break;
				}
			}
		} else {
			if ("1011".equals(role) || "1".equals(role)) {
				flag = true;
			}
		}
		return flag;
	}

	// 获取摘要
	@RequestMapping("/getProductExplain")
	public List<String> getProductExplain(@RequestParam("productExplain") String productExplain) {
		List<String> name = customerDataService.getProductExplain(productExplain);
		return name;
	}
}
