package com.ssj.sys.controller;

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

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssj.bean.common.framework.core.domain.Response;
import com.ssj.bean.sys.award.domain.AwardMaterial;
import com.ssj.bean.sys.award.domain.AwardSncode;
import com.ssj.bean.sys.fx.domain.Merchant;
import com.ssj.bean.sys.role.domain.Admin;
import com.ssj.framework.basic.common.bean.SplitPage;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.service.sys.award.service.AwardActivityLogService;
import com.ssj.service.sys.award.service.AwardMaterialService;
import com.ssj.service.sys.award.service.AwardSncodeService;
import com.ssj.service.sys.fx.service.MerchantService;
import com.ssj.sys.util.thirdparty.excel.CustomizeToExcel;
import com.ssj.sys.util.thirdparty.excel.ExcelColumn;

/**
 *    新版的抽奖后台管理功能
 * 2020年5月27日9:37:1
 * @author wuwen
 */

@Controller
@RequestMapping("/sys/award")
public class AwardController extends BaseController {
	
    @Autowired
    private AwardMaterialService awardMaterialService;
    
    @Autowired
    private AwardActivityLogService awardActivityLogService;
   
	@Autowired
	private MerchantService merchantService;
	
	@Autowired
	private AwardSncodeService awardSncodeService;
	
	//指点的code
	public static String SSJCODE="ssj001";
	
	//管理层的org
	public static String SSJFX="SSJ-FX";
	
	//奖品物资列表
	@RequestMapping("/material/list")
    public String materialList(Model model,
                                        @RequestParam(required = false, defaultValue = "30") int pageSize,
                                        @RequestParam(required = false, defaultValue = "1") int pageNo) {
        Map<String, Object> params = new HashMap<String, Object>();
        
    	HttpSession session = request.getSession();
		Admin admin =(Admin) session.getAttribute("admin");
		Merchant curMerchant=merchantService.getMerchantByPosCode(admin.getLoginName());
        
        params.put("name", request.getParameter("name")==null?"":request.getParameter("name"));
        params.put("level", request.getParameter("level")==null?"":request.getParameter("level"));
        
        SplitPage sp = new SplitPage();
        sp.setAction(request.getRequestURI());
        sp.setPageNo(pageNo);
        sp.setPageSize(pageSize);
        sp.setParams(params);
        Page<Map<String,Object>> page = awardMaterialService.findAwardMaterialBylist(params, initPage(pageNo, pageSize));
        List<Map<String,Object>> list=page.getContent();
       
        sp.setRowCnt((int) page.getTotalElements());
        model.addAttribute("curMerchant", curMerchant);
        model.addAttribute("list",list);
        model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
        model.addAttribute("page", sp);
        model.addAttribute("search", params);
        return "sys/award/material/list";
    }
	
	
	//导出奖品物资列表
	@RequestMapping("/material/list_export")
    @ResponseBody
    public void materialListExport(Model model) throws Exception {
		
		 Map<String, Object> params = new HashMap<String, Object>();
	        
        params.put("name", request.getParameter("name")==null?"":request.getParameter("name"));
        params.put("level", request.getParameter("level")==null?"":request.getParameter("level"));
        
        Page<Map<String,Object>> page = awardMaterialService.findAwardMaterialBylist(params, initPage(1, 10000));
        List<Map<String,Object>> list=page.getContent();
       
        String libName="奖品物资列表";
        ServletOutputStream os = response.getOutputStream(); //获得输出流  
		response.reset();   //清空输出流  
		String fileName = new String(libName.getBytes("gb2312"), "ISO8859-1") +".xlsx";  
		response.setHeader("Content-disposition", "attachment; filename="+ fileName); //设定输出文件头  
		response.setContentType("application/vnd.ms-excel;charset=UTF-8");
		response.setCharacterEncoding("utf-8");
		
        for (Map<String, Object> map : list) {
			switch (map.get("level").toString()) {
				case "1":map.put("level", "一等奖");break;
				case "2":map.put("level", "二等奖");break;
				case "3":map.put("level", "三等奖");break;
				default:map.put("level", "");break;
			}
		}
	    List<ExcelColumn> myColumns3 = new ArrayList<ExcelColumn>();
		myColumns3.add(new ExcelColumn("奖品名称", "name", 20));
		myColumns3.add(new ExcelColumn("等级", "level", 20));
		myColumns3.add(new ExcelColumn("录入总库存", "stock_number", 20));
		myColumns3.add(new ExcelColumn("已用库存", "new_use_number", 20));
		myColumns3.add(new ExcelColumn("未用库存", "not_use_number", 20));
		myColumns3.add(new ExcelColumn("兑换积分", "exchange_integral", 20));
		myColumns3.add(new ExcelColumn("市场价", "price", 20));
		myColumns3.add(new ExcelColumn("奖品备注", "remarks", 20));
		myColumns3.add(new ExcelColumn("创建时间", "create_time", 20));
    	CustomizeToExcel.downFile(myColumns3, list, os);
    }
	
	
	//奖品物资新增，编辑
	@RequestMapping("/material/edit")
    public String materialEdit(Model model,String id) throws Exception {
		AwardMaterial awardMaterial=new AwardMaterial();
		if(StringUtils.isNotEmpty(id)) {
			awardMaterial=awardMaterialService.getById(id);
		}
		model.addAttribute("awardMaterial", awardMaterial);
        return "sys/award/material/edit";
    }
	
	
	
	//奖品物资新增，编辑，保存
	@RequestMapping("/material/save")
	@ResponseBody
    public Response materialSave(Model model,AwardMaterial awardMaterial)throws Exception {
		Response response = new  Response();
		if(StringUtils.isEmpty(awardMaterial.getId())) {
			awardMaterial.setStockNumber(0);
//			awardMaterial.setUseNumber(0);
//			awardMaterial.setLockNumber(0);
			awardMaterial.setStatus(1);
			awardMaterial.setCreateTime(new Date());
			awardMaterial.setUpdateTime(new Date());
			awardMaterialService.save(awardMaterial);
		}else {
			AwardMaterial oldAwardMaterial=awardMaterialService.getById(awardMaterial.getId());
			oldAwardMaterial.setName(awardMaterial.getName());
			oldAwardMaterial.setImg(awardMaterial.getImg());
			oldAwardMaterial.setLevel(awardMaterial.getLevel());
			oldAwardMaterial.setExchangeIntegral(awardMaterial.getExchangeIntegral());
			oldAwardMaterial.setUpdateTime(new Date());
			oldAwardMaterial.setRemarks(awardMaterial.getRemarks());
			oldAwardMaterial.setPrice(awardMaterial.getPrice());
			awardMaterialService.save(oldAwardMaterial);
		}
		response.success("成功");
        return response;
    }
	
	
	//奖品物资的状态更新
	@RequestMapping("/material/status")
	@ResponseBody
    public Response materialStatus(Model model,String id,Integer status)throws Exception {
		Response response = new  Response();
		if(StringUtils.isNotEmpty(id)) {
			AwardMaterial awardMaterial = awardMaterialService.getById(id);
			awardMaterial.setStatus(status);
			awardMaterial.setUpdateTime(new Date());
			awardMaterialService.save(awardMaterial);
		}
		response.success("成功");
        return response;
    }
	
	
	//奖品物资新增库存
	@RequestMapping("/material/number")
    public String materialNumber(Model model,String id) throws Exception {
		AwardMaterial awardMaterial=new AwardMaterial();
		if(StringUtils.isNotEmpty(id)) {
			awardMaterial=awardMaterialService.getById(id);
		}
		model.addAttribute("awardMaterial",awardMaterial);
        return "sys/award/material/number";
    }
	
	
	//奖品物资新增库存，保存
	@RequestMapping("/material/number/save")
	@ResponseBody
    public Response materialNumberSave(Model model,String id,Integer number)throws Exception {
		Response response = new  Response();
		if(StringUtils.isNotEmpty(id)) {
			if(number==null) {
				number=0;
			}
			awardMaterialService.addNumberAndSnCode(id, number);
		}
		response.success("成功");
        return response;
    }
	
	//编辑Ems
	@RequestMapping("/sncode/ems")
    public String grantEmsEdit(Model model,String id) throws Exception {
		AwardSncode awardSncode=new AwardSncode();
		if(StringUtils.isNotEmpty(id)) {
			awardSncode=awardSncodeService.getById(id);
		}
		model.addAttribute("awardSncode", awardSncode);
        return "sys/award/sncode/ems";
    }
		
		
	//奖品物流的保存
	@RequestMapping("/sncode/ems/save")
	@ResponseBody
    public Response grantEmsSave(Model model,String id,String dingNumber,String emsNumber,String people,String telephone,String address,String remarks,String img) throws Exception {
		Response response = new  Response();
		AwardSncode awardSncode=new AwardSncode();
		if(StringUtils.isNotEmpty(id)) {
			awardSncode=awardSncodeService.getById(id);
			awardSncode.setDingNumber(dingNumber);
			awardSncode.setEmsNumber(emsNumber);
			awardSncode.setPeople(people);
			awardSncode.setTelephone(telephone);
			awardSncode.setAddress(address);
			awardSncode.setEmsTime(new Date());
			awardSncode.setImg(img);
			awardSncodeService.save(awardSncode);
			response.success("成功");
		}else {
			response.failure("保存物流的异常");
		}
		return response;
    }



	@RequestMapping("/sncode/list")
    public String sncodeList(Model model,
			            @RequestParam(required = false, defaultValue = "30") int pageSize,
			            @RequestParam(required = false, defaultValue = "1") int pageNo) throws Exception {

	 	Map<String, Object> params = new HashMap<String, Object>();
        params.put("awardMaterialId", request.getParameter("awardMaterialId")==null?"":request.getParameter("awardMaterialId"));
        params.put("leagueName", request.getParameter("leagueName")==null?"":request.getParameter("leagueName"));
        params.put("snCode", request.getParameter("snCode")==null?"":request.getParameter("snCode"));
        params.put("name", request.getParameter("name")==null?"":request.getParameter("name"));
        params.put("level", request.getParameter("level")==null?"":request.getParameter("level"));
        params.put("status",request.getParameter("status")==null?"":request.getParameter("status"));
        params.put("emsstatus",request.getParameter("emsstatus")==null?"":request.getParameter("emsstatus"));
        params.put("order",request.getParameter("order")==null?"create_time":request.getParameter("order"));
        
        
        SplitPage sp = new SplitPage();
        sp.setAction(request.getRequestURI());
        sp.setPageNo(pageNo);
        sp.setPageSize(pageSize);
        sp.setParams(params);
        Page<Map<String,Object>> page = awardSncodeService.findAwardSncodeBylist(params, initPage(pageNo, pageSize));
        List<Map<String,Object>> list=page.getContent();
       
        sp.setRowCnt((int) page.getTotalElements());
        model.addAttribute("list",list);
        model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
        model.addAttribute("page", sp);
        model.addAttribute("search", params);
        return "sys/award/sncode/list";
    }

	
	
	
}
