package lingtao.net.service;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lingtao.net.bean.CustomerData;
import lingtao.net.bean.Msg;
import lingtao.net.bean.SysUser;
import lingtao.net.dao.CustomerDataMapper;
import lingtao.net.util.DateFormatUtils;

@Service
public class CustomerDataService {

	@Autowired
	private CustomerDataMapper customerDatamapper;

	/**
	 * 根据角色身份查询数据列表
	 */
	public List<CustomerData> getCustomerDatas(CustomerData customerData) {
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		customerData.setCreateDateEnd(new DateFormatUtils().formatEndTime(customerData.getCreateDateBegin()));
		customerData.setCreateDateBegin(new DateFormatUtils().formatBeginTime(customerData.getCreateDateBegin()));
		// 用户所拥有的角色
		String[] roleArr = user.getRole().split(",");
		// 判断是否拥有超管身份标识
		boolean isSuper = false;
		for (int i = 0; i < roleArr.length; i++) {
			// 有‘超级管理员’身份,状态改为true
			if ("1".equals(roleArr[i]) || "777".equals(roleArr[i])) {
				isSuper = true;
				break;
			}
		}

		// 如果没有‘超级管理员’身份，就展示所拥有的店铺数据;如果有就不加条件，直接查询所有
		if (!isSuper) {
			// 如果有多个
			if (user.getRole().contains(",")) {
				customerData.setRoleArr(roleArr);
			} else {// 只有一个角色
				customerData.setRoleSearch(user.getRole());
			}
		}

		return customerDatamapper.getCustomerDatas(customerData);
	}

	/**
	 * 新增数据
	 * 
	 * @param customerData
	 */
	public void addCustomerData(CustomerData customerData) {
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		// 未成交数据
		customerData.setIsBuy("0");
		customerData.setUsername(user.getUsername());
		customerData.setRealname(user.getRealname());
		customerData.setCreateBy(user.getRealname());
		customerDatamapper.addCustomerData(customerData);
	}

	/**
	 * 修改数据
	 * 
	 * @param customerData
	 */
	public void updateCustomerDataById(CustomerData customerData) {
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		customerData.setUpdateBy(user.getRealname());
		customerDatamapper.updateCustomerDataById(customerData);
	}

	/**
	 * 删除数据
	 * 
	 * @param id
	 */
	public void deleteCustomerDataById(Integer id) {
		customerDatamapper.deleteCustomerDataById(id);
	}

	/**
	 * 修改 店长评语
	 * 
	 * @param id
	 * @param field
	 * @param value
	 */
	public void updateRemarkById(int id, String field, String value) {
		CustomerData customerData = new CustomerData();
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		if ("commentManager".equals(field)) {
			customerData.setId(id);
			customerData.setCommentManager(value);
			customerData.setUpdateBy(user.getRealname());
		}
		customerDatamapper.updateCommentManager(customerData);
	}

	/**
	 * 修改完成状态
	 * 
	 * @param id
	 * @return
	 */
	public Msg changeIsBuy(Integer id) {
		try {
			customerDatamapper.changeIsBuy(id);
			return Msg.success();
		} catch (Exception e) {
			return Msg.fail();
		}
	}

	/**
	 * 查询产品说明报价记录
	 * 
	 * @param productExplain
	 * @return
	 */
	public List<String> getProductExplain(String productExplain) {
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		return customerDatamapper.getProductExplain(productExplain, user.getUsername());
	}

}
