package com.wechat.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wechat.common.utils.StringsUtils;
import com.wechat.global.message.InfoMsg;
import com.wechat.model.dbEntity.MdeAddress;
import com.wechat.model.dto.AddressesDto;
import com.wechat.model.dto.GoodsDetailDto;
import com.wechat.model.dto.GoodsListDto;
import com.wechat.model.dto.UpTimeGroupDto;
import com.wechat.model.responseDto.GoodsListInitResp;
import com.wechat.model.responseDto.ResultEntity;
import com.wechat.service.CommonService;
import com.wechat.service.MdeAddressService;
import com.wechat.service.MdeGoodsService;

@Controller
@RequestMapping(value = "goods")
public class GoodsController {

	@Autowired
	MdeGoodsService goodService;
	
	@Autowired
	MdeAddressService  addressService;

	@Autowired
	CommonService commonService;

	/**
	 * 商品列表页初始化
	 * @return
	 */
	@RequestMapping(value = "init", method = RequestMethod.GET)
	@ResponseBody
	public ResultEntity<GoodsListInitResp> initGoodsPage(HttpServletRequest request) {
		ResultEntity<GoodsListInitResp> result = new ResultEntity<GoodsListInitResp>();
		GoodsListInitResp resp = new GoodsListInitResp();
		
		String lang = commonService.getLanguage(request);
		
		//获取上架时间分组
		List<UpTimeGroupDto> upTimeGroupList = goodService.getUpTimeGroup();
		
		// 获取显示的上架时间
		UpTimeGroupDto showTimeDto = goodService.getUpTimeShow();
		Date showTime = null;
		
		// 根据上架时间和上架状态获取商品列表
		List<GoodsListDto> goodsList = new ArrayList<GoodsListDto>();
		if(showTimeDto != null)
		{
			//有当天的场次的情况
			showTime = showTimeDto.getUpTime();
			goodsList = goodService.getGoodsListByUpTime(showTime,showTimeDto.getOnSale(), lang);
		} else if(upTimeGroupList != null){
			//当天的在售和热销都没有的情况
			showTime = upTimeGroupList.get(0).getUpTime();
			goodsList = goodService.getGoodsListByUpTime(null,upTimeGroupList.get(0).getOnSale(), lang);
		}
		
		//商品销量赋值
		goodService.setGoodsSale(goodsList);
		
		//结果封装
		resp.setUpTimeGroupList(upTimeGroupList);
		resp.setShowTime(showTime);
		resp.setGoodsList(goodsList);
		resp.setStandardTime(new Date());
		result.setResult(resp);
		return result;
	}
	
	/**
	 * 商品列表页查询
	 * @return
	 */
	@RequestMapping(value = "getPartOfGoodsList", method = RequestMethod.POST)
	@ResponseBody
	public ResultEntity<List<GoodsListDto>> getPartOfGoodsList(HttpServletRequest request,@RequestBody UpTimeGroupDto upTimeReq) {
		ResultEntity<List<GoodsListDto>> result = new ResultEntity<List<GoodsListDto>>();
		String lang = commonService.getLanguage(request);
		
		// 根据上架时间和上架状态获取商品列表
		List<GoodsListDto> goodsList = new ArrayList<GoodsListDto>();
		String onSale = upTimeReq.getOnSale();
		if(StringsUtils.isEmpty(onSale) ||(!"1".equals(onSale)&& !"2".equals(onSale)))
		{
			result.setMsg(InfoMsg.ERROR_PARAMS_ERROR);
		}else {
			goodsList =  goodService.getGoodsListByUpTime(upTimeReq.getUpTime(),onSale, lang);
		}

		//商品销量赋值
		goodService.setGoodsSale(goodsList);
		
		//结果封装
		result.setResult(goodsList);
		return result;
	}
	
	/**
	 *  商品详情查询
	 * @return
	 */
	@RequestMapping(value = "getGoodsDetail", method = RequestMethod.GET)
	@ResponseBody
	public ResultEntity<GoodsDetailDto> getGoodsDetail(HttpServletRequest request,@RequestParam int goodsId) {
		ResultEntity<GoodsDetailDto> result = new ResultEntity<GoodsDetailDto>();
		String lang = commonService.getLanguage(request);
		
		// 获取商品详情
		GoodsDetailDto goodsDetail = goodService.getGoodsInfo(goodsId, lang);
		//结果封装
		result.setResult(goodsDetail);
		return result;
	}
	
	/**
	 *  根据用户id查询配送地址
	 * @return
	 */
	@RequestMapping(value = "getAddressesByUser", method = RequestMethod.GET)
	@ResponseBody
	public ResultEntity<List<AddressesDto>> getAddressesByUser(HttpServletRequest request) {
		ResultEntity<List<AddressesDto>> result = new ResultEntity<List<AddressesDto>>();
		String userid = commonService.getUserCode(request);
		
		// 获取配送地址
		List<AddressesDto> adds = addressService.getAddressesByUser(userid);
		//结果封装
		result.setResult(adds);
		return result;
	}
	
	
	/**
	 *  新增、修改查询地址
	 * @return
	 */
	@RequestMapping(value = "updAddress", method = RequestMethod.POST)
	@ResponseBody
	public ResultEntity<Integer> updAddress(HttpServletRequest request,@RequestBody AddressesDto addDto) {
		ResultEntity<Integer> result = new ResultEntity<Integer>();
		String userId = commonService.getUserCode(request);

		//将entity和入参相同属性复制至entity中
		MdeAddress entity = new MdeAddress();
		BeanUtils.copyProperties(addDto, entity);
		entity.setUserId(userId);
		
		int count = addressService.updAddress(entity);
		if(count < 1)
		{
			result.setMsg(InfoMsg.ERROR_DATA_UPDATE);
		}
		
		//返回主key
		result.setResult(entity.getAddId());
		return result;
	}
	

	/**
	 *删除 用户地址
	 * 
	 * @param addId 地址id
	 */
	@RequestMapping(value = "delAddress", method = RequestMethod.POST)
	@ResponseBody
	public ResultEntity<String> deleteOrderByUser(@RequestParam Integer addId, HttpServletRequest req){
		ResultEntity<String> result = new ResultEntity<String>();
		String userCode = commonService.getUserCode(req);
		int count = 0;
		if(addId == null) {
			result.setMsg(InfoMsg.ERROR_EMPTY_REQUEST);
		} else {
			count = addressService.delete(addId, userCode);
		}
		
		if (count < 1)
		{
			result.setMsg(InfoMsg.ERROR_DATA_UPDATE);
		}
		
		return result;
	}

}
