package lingtao.net.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

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

import lingtao.net.bean.Msg;
import lingtao.net.bean.QuoteData;
import lingtao.net.bean.SysUser;
import lingtao.net.service.QuoteDataService;

@RestController
public class QuoteDataController {

	@Autowired
	private QuoteDataService quoteDataService;

	/**
	 * 获取操作日志列表
	 * 
	 * @param page
	 * @param limit
	 * @param quoteLog
	 * @return
	 */
	@RequestMapping("/getQuoteData")
	public PageInfo<QuoteData> logList(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit, QuoteData quoteData) {
		PageHelper.startPage(page, limit);
		List<QuoteData> logList = quoteDataService.quoteDatas(quoteData);
		PageInfo<QuoteData> pageInfo = new PageInfo<QuoteData>(logList);
		return pageInfo;
	}

	/**
	 * 修改数据
	 * 
	 */
	@RequestMapping("/updateById")
	public Msg updateById(@RequestParam(value = "id") int id, @RequestParam(value = "field") String field,
			@RequestParam(value = "value") String value, @RequestParam(value = "username") String username) {
		return quoteDataService.updateById(id, field, value, username);
	}

	/**
	 * 获取报过的产品
	 * 
	 */
	@RequestMapping("/getProTypeLabel")
	public List<String> getProType() {
		return quoteDataService.getProType();
	}

	/**
	 * 修改【是否当天成交】状态(自己修改)
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/changeIsBuyToDayStatus")
	public Msg changeIsBuyToDay(@RequestParam(value = "id") Integer id,
			@RequestParam(value = "username") String username) {
		// 只有自己的数据才能更改【是否当天成交】状态
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		// 超管、组长身份也能更改【是否当天成交】状态
		boolean flag = new CustomerDataControlle().isSuperOrManager();
		if (!username.equals(user.getUsername()) && !flag) {
			return Msg.fail("只允许修改自己的数据！");
		}
		return quoteDataService.changeIsBuyToDay(id);
	}

	/**
	 * 修改【是否成交】状态(店长修改)
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/changeIsBuyStatus")
	public Msg changeIsBuy(@RequestParam(value = "id") Integer id) {
		// 超管、组长身份才允许修改【成交状态】
		boolean flag = new CustomerDataControlle().isSuperOrManager();
		if (!flag) {
			return Msg.fail();
		}
		return quoteDataService.changeIsBuy(id);
	}

	/**
	 * 修改所属店铺以及选择状态
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/updateShopname")
	public Msg updateShopname(@RequestParam(value = "id") Integer id, @RequestParam(value = "shopname") String shopname,
			@RequestParam(value = "username") String username) {
		if (StringUtils.isEmpty(shopname)) {
			return Msg.fail("请选择有效店铺！");
		}
		// 只有自己的数据才能更改【所属店铺】
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		if (!username.equals(user.getUsername())) {
			return Msg.fail("只允许修改自己的数据！");
		}
		return quoteDataService.updateShopnameSelect(shopname, id);
	}

	@RequestMapping("/addOrderNumber")
	public Msg addOrderNumber(@RequestParam(value = "id") Integer id,
			@RequestParam(value = "orderNumber") String orderNumber) {
		if (StringUtils.isEmpty(orderNumber)) {
			return Msg.fail("请填写订单号");
		}
		if (orderNumber.length() < 10 || orderNumber.length() > 25) {
			return Msg.fail("请填写正确的订单号");
		}
		return quoteDataService.addOrderNumber(id, orderNumber);
	}

	/**
	 * 柱状图
	 * 
	 */
	@ResponseBody
	@RequestMapping("/echartZhuSummary")
	public Map<String, Object> echartZhuList(QuoteData quoteData) {
		return quoteDataService.echartZhuList(quoteData, 0);
	}
	
	/**
	 * 客服大单统计图
	 * 
	 */
	@ResponseBody
	@RequestMapping("/echartKefuSummary")
	public Map<String, Object> echartKefuList(QuoteData quoteData) {
		return quoteDataService.echartZhuList(quoteData, 1);
	}

	/**
	 * 导出
	 * 
	 * @param response
	 * @param request
	 * @param difference
	 * @throws Exception
	 */
	@RequestMapping("/excel_quoteData")
	public void excel(HttpServletResponse response, QuoteData quoteData) throws Exception {
		quoteDataService.excel(response, quoteData);
	}
}
