package com.cloudcross.ssp.web.advertiser.main.ad.target;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.ArrayUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloudcross.ssp.model.Account;
import com.cloudcross.ssp.model.AdGroup;
import com.cloudcross.ssp.model.Advertiser;
import com.cloudcross.ssp.model.Banner;
import com.cloudcross.ssp.model.BannerTemplate;
import com.cloudcross.ssp.model.Log;
import com.cloudcross.ssp.model.Order;
import com.cloudcross.ssp.model.Size;
import com.cloudcross.ssp.service.IAdGroupService;
import com.cloudcross.ssp.service.IAdvertiserService;
import com.cloudcross.ssp.service.IBannerService;
import com.cloudcross.ssp.service.IBannerTemplateService;
import com.cloudcross.ssp.service.ILogService;
import com.cloudcross.ssp.service.IOrderService;
import com.cloudcross.ssp.service.ISizeService;
import com.cloudcross.ssp.service.impl.BannerService;
import com.cloudcross.ssp.base.web.SimpleController;
import com.cloudcross.ssp.common.utils.Pager;









/**
 * 从创意中心批量克隆创意。
 * 
 * @author jiapeng
 */
@Controller
@RequestMapping("/advertiser/main/ad/target/clone-banner")
public class CloneBannerController extends SimpleController {
	private Logger LOG = Logger.getLogger(CloneBannerController.class);
	@Autowired
	IBannerTemplateService bat;
	@Autowired
	IBannerService bs;
	@Autowired
	IAdGroupService adGroupService;
	@Autowired
	IOrderService orderService;
	@Autowired
	IAdvertiserService advs;
	@Autowired
	ISizeService sizeService;
	

	@RequestMapping
	public String index(Model model,
			@RequestParam Map<String,Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {
		
		Long adGroupId = Long.parseLong((String)paramMap.get("adGroupId"));
		AdGroup adGroup = adGroupService.findById(adGroupId);
		Long campaignId = adGroup.getCampaignId();
		Long orderId = adGroup.getOrderId();
		Order order = orderService.findById(orderId);
		Long advertiserId = order.getAdvertiserId();	
		paramMap.put("advertiserId", advertiserId);
		int totalRow = bat.countByParams3(paramMap);
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);

		
		List<Size> sizeList = sizeService.findAllSize();
		System.out.println("size111=="+sizeList);
		List<BannerTemplate> bannerTemplateList = bat.findByParams4(paramMap, pager);
		LOG.debug("bannertemplate="+bannerTemplateList);

		model.addAttribute("pager", pager);
		model.addAttribute("sizeList",sizeList);
		model.addAttribute("bannerTemplateList", bannerTemplateList);
		model.addAttribute("adGroupId", paramMap.get("adGroupId"));
		model.addAllAttributes(paramMap);
		return page("index");
	}


	@RequestMapping("/list")
	public String list(Model model, @RequestParam Map<String, Object> paramMap,
			@RequestParam(defaultValue = "1") int page) {
		LOG.debug("paramm111=="+paramMap);
		Long adGroupId = Long.parseLong((String)paramMap.get("adGroupId"));
		model.addAttribute("width",paramMap.get("width"));
		model.addAttribute("height",paramMap.get("height"));
		model.addAttribute("type",paramMap.get("type"));
		// 根据广告策略名模糊统计符合条件的策略总数
		AdGroup adGroup = adGroupService.findById(adGroupId);
		Long campaignId = adGroup.getCampaignId();
		Long orderId = adGroup.getOrderId();
		Order order = orderService.findById(orderId);
		Long advertiserId = order.getAdvertiserId();
		paramMap.put("advertiserId", advertiserId);
		int totalRow = bat.countByParams3(paramMap);
		LOG.debug("total===="+totalRow+"paramMap=="+paramMap);
		// 查询符合条件的广告策略
		Pager pager = new Pager();
		pager.setPage(page);
		pager.setTotalRow(totalRow);		
		List<BannerTemplate> bannerTemplateList = bat.findByParams4(paramMap, pager);
		LOG.debug("bannertemplate="+bannerTemplateList);
		model.addAttribute("pager", pager);
		model.addAttribute("bannerTemplateList", bannerTemplateList);
		model.addAttribute("adGroupId", adGroupId);
		model.addAllAttributes(paramMap);
		return page("list");
	}

	/**
	 * 从创意中心取得创意模板数据保存到创意表中。
	 */
	@RequestMapping("/save")
	public String save(Model model,HttpServletRequest request, Long adGroupId, Long[] id) {
		LOG.debug("请求参数adTemplateId:" + Arrays.toString(id));
		Map<String, Object> paramterMap = new HashMap<String, Object>();
		AdGroup adGroup = adGroupService.findById(adGroupId);
		Long advertiserId = adGroup.getAdvertiserId();
		Advertiser advertiser = advs.findById(advertiserId);
		Long agentId = advertiser.getAgentId();
		
		//下面几个参数用来当添加创意成功后添加日志用的
		Account account = getLoginUser();
		paramterMap.put("account", account);
		paramterMap.put("request", request);
		
		paramterMap.put("advertiserId", advertiserId);
		paramterMap.put("agentId", agentId);
		paramterMap.put("adGroupId", adGroup.getId());
		paramterMap.put("campaignId", adGroup.getCampaignId());
		paramterMap.put("orderId", adGroup.getOrderId());
		if (ArrayUtils.isNotEmpty(id)) {
			for (Long ids : id) {
				paramterMap.put("bannerTemplateId", ids);
				((BannerService) bs).cloneBanner(paramterMap);
				
			}
		}
	
		model.addAttribute("adGroupId", adGroupId);
		return redirect(page("../create-banner/list"));
	}
}

