package com.iamberry.wechat.handles.channel;

import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.banner.MessageDTO;
import com.iamberry.wechat.core.entity.channel.ChannelRebate;
import com.iamberry.wechat.core.entity.channel.ChildChannel;
import com.iamberry.wechat.core.entity.channel.MainChannel;
import com.iamberry.wechat.core.entity.coupon.AdminCouponItemDto;
import com.iamberry.wechat.core.entity.page.PageRequest;
import com.iamberry.wechat.core.entity.page.PagedResult;
import com.iamberry.wechat.core.entity.receive.ChargerReceive;
import com.iamberry.wechat.face.channel.ChannelRebateService;
import com.iamberry.wechat.face.channel.ChildChannelService;
import com.iamberry.wechat.face.channel.MainChannelService;
import com.iamberry.wechat.face.coupon.AdminCouponItemService;
import com.iamberry.wechat.tools.NameUtils;
import com.iamberry.wechat.tools.ResultInfo;
import com.iamberry.wechat.utils.StitchAttrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin/channel")
public class AdminChannelHandler {
	
	@Autowired
	private ChannelRebateService channelRebateService;
	@Autowired
	private MainChannelService mainChannelService;
	@Autowired
	private ChildChannelService childChannelService;

	/**
	 * 查询返利列表
	 *
	 * @return
	 * @author LJK
	 * @date 时间
	 */
	@RequestMapping("/listChannelRebate")
	public ModelAndView listChannelRebate(HttpServletRequest request, ChannelRebate channelRebate,
										  @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
										  @RequestParam(value = "pageNO", defaultValue = "1", required = false) Integer pageNO,
										  @RequestParam(value = "totalNum", defaultValue = "0", required = false) Integer totalNum) {
		ModelAndView mv = new ModelAndView("/admin/adminChannel/channelRebate_list");
       /* if(customerBasicInfo.getIsLookAll() != null || !customerBasicInfo.getIsLookAll().equals("")){
            customerBasicInfo.setIdCreateBy(adminId);
        }*/
		DateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");
		String stateDate = request.getParameter("stateDates");
		String endDate = request.getParameter("endDates");
		if(stateDate != null &&  !stateDate.equals("")){
			try {
				channelRebate.setStateDate(sdf.parse(stateDate));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if(endDate != null &&  !endDate.equals("")){
			try {
				channelRebate.setEndDate(sdf.parse(endDate));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		// 封装请求数据
		PageRequest<ChannelRebate> pageRequest = new PageRequest<>(channelRebate, pageNO, pageSize, totalNum == 0);
		// 查询订单列表
		PagedResult<ChannelRebate> result = channelRebateService.getChannelRebateList(pageRequest);
		if (totalNum != 0) {
			result.setTotal(totalNum);
		}
		StitchAttrUtil sa = StitchAttrUtil.getSa();
		sa.addDatePro("yyyy-MM-dd","stateDate","endDate");
		sa.setModelAndView(channelRebate, mv, "/admin/channel/listChannelRebate", result);
		return mv;
	}

	/**
	 * 修改状态
	 * 2018年2月24日
	 * @author
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateState")
	public ResultMsg update_state(ChannelRebate channelRebate, HttpServletRequest request) throws Exception {
		ResultMsg msg = new ResultMsg();
		if(channelRebate.getRebateId() == null  || channelRebate.getRebackStatus() == null || "".equals(channelRebate.getRebateId())
				|| channelRebate.getRebackStatus() == null){
			msg.setMessage(NameUtils.getConfig("ERRORCODE"));
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
			return msg;
		}
		channelRebate.setRebackTime(new Date());
		Integer flag = channelRebateService.update(channelRebate);
		if(flag == 1){
			msg.setMessage(NameUtils.getConfig("SUCCESSINFO"));
			msg.setResultCode(ResultInfo.SUCCESSCODE);
			msg.setStatus(true);
			return msg;
		}else{
			msg.setMessage(NameUtils.getConfig("ERRORCODE"));
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
			return msg;
		}
	}


	/**
	 * 查询主渠道列表
	 *
	 * @return
	 * @author LJK
	 * @date 时间
	 */
	@RequestMapping("/listMainChannel")
	public ModelAndView listMainChannel(HttpServletRequest request, MainChannel mainChannel,
										  @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
										  @RequestParam(value = "pageNO", defaultValue = "1", required = false) Integer pageNO,
										  @RequestParam(value = "totalNum", defaultValue = "0", required = false) Integer totalNum) {
		ModelAndView mv = new ModelAndView("/admin/adminChannel/main_channel_list");
		// 封装请求数据
		PageRequest<MainChannel> pageRequest = new PageRequest<>(mainChannel, pageNO, pageSize, totalNum == 0);
		// 查询订单列表
		PagedResult<MainChannel> result = mainChannelService.getMainChannelList(pageRequest);
		if (totalNum != 0) {
			result.setTotal(totalNum);
		}
		StitchAttrUtil sa = StitchAttrUtil.getSa();
		sa.setModelAndView(mainChannel, mv, "/admin/channel/listMainChannel", result);
		return mv;
	}

	/**
	 * 查询子渠道列表
	 *
	 * @return
	 * @author LJK
	 * @date 时间
	 */
	@RequestMapping("/listChildChannel")
	public ModelAndView listCMainChannel(HttpServletRequest request, ChildChannel childChannel,
										@RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
										@RequestParam(value = "pageNO", defaultValue = "1", required = false) Integer pageNO,
										@RequestParam(value = "totalNum", defaultValue = "0", required = false) Integer totalNum) {
		ModelAndView mv = new ModelAndView("/admin/adminChannel/child_channel_list");
		// 封装请求数据
		PageRequest<ChildChannel> pageRequest = new PageRequest<>(childChannel, pageNO, pageSize, totalNum == 0);
		// 查询订单列表
		PagedResult<ChildChannel> result = childChannelService.getChildChannelList(pageRequest);
		if (totalNum != 0) {
			result.setTotal(totalNum);
		}
		StitchAttrUtil sa = StitchAttrUtil.getSa();
		sa.setModelAndView(childChannel, mv, "/admin/channel/listChildChannel", result);
		return mv;
	}
	/**
	 * 结算
	 *
	 * @return
	 * @author LJK
	 */
	@ResponseBody
	@RequestMapping("/settlement")
	public ResultMsg settlement(HttpServletRequest request) {
		ResultMsg rm = ResultMsg.getSuccess();
		String[] rebates = request.getParameterValues("rebates");
		String channelId = request.getParameter("channelId");//渠道id
		String channelType = request.getParameter("channelType");//渠道id
		if(rebates == null || channelId == null || channelType == null ||
				channelId.equals("")||channelType.equals("")	){
			rm.setStatus(false);
			return rm;
		}
		ChannelRebate channelRebate = new ChannelRebate();
		channelRebate.setRebates(rebates);
		int sum = channelRebateService.getChannelRebateSum(channelRebate);
		if (channelType.equals("1")){
			MainChannel mainChannel = mainChannelService.getMainChannelById(Integer.valueOf(channelId));
			rm.setStatus(true);
			mainChannel.setRebackAmount(sum);
			rm.addData("channel",mainChannel);
		}else {
			ChildChannel childChannel = childChannelService.getChildChannelById(Integer.valueOf(channelId));
			rm.setStatus(true);
			childChannel.setRebackAmount(sum);
			rm.addData("channel",childChannel);
		}
		rm.addData("rebates",rebates);
		return rm;
	}

	/**
	 * 修改状态
	 * 2018年2月24日
	 * @author
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateRebackStatus")
	public ResultMsg updateRebackStatus(ChannelRebate channelRebate, HttpServletRequest request) throws Exception {
		ResultMsg msg = new ResultMsg();
		if(channelRebate.getRebates() == null){
			msg.setMessage(NameUtils.getConfig("ERRORCODE"));
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
			return msg;
		}
		Integer flag = channelRebateService.updateRebackStatus(channelRebate);
		if(flag > 0){
			msg.setMessage(NameUtils.getConfig("SUCCESSINFO"));
			msg.setResultCode(ResultInfo.SUCCESSCODE);
			msg.setStatus(true);
			return msg;
		}else{
			msg.setMessage(NameUtils.getConfig("ERRORCODE"));
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
			return msg;
		}
	}


	/**
	 * 添加返利页面
	 *
	 * @return
	 * @author LJK
	 * @date 时间
	 */
	@RequestMapping("/toAddChannel")
	public ModelAndView toAddChannel(HttpServletRequest request,Integer type) {
		ModelAndView mv = new ModelAndView("/admin/adminChannel/addChannel");
		mv.addObject("type",type);
		return mv;
	}

}
