package org.cs.mgr.product.ctl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

import org.cs.core.ctl.BaseCtl;
import org.cs.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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 org.cs.mgr.product.service.IMgmProductCategoryService;
import org.cs.util.Pager;
import org.cs.mgr.admin.model.Menu;
import org.cs.mgr.admin.service.IDCProjectService;
import org.cs.mgr.admin.vo.ZTree;
import org.cs.mgr.product.model.MgmProductCategory;
import org.cs.util.StringUtil;
import com.alibaba.fastjson.JSONObject;


/**
 * 
 * @ClassName: MgmProductCategory 
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:29:09
 */
@Controller
@RequestMapping("/mgmProductCategoryCtl")
public class MgmProductCategoryCtl extends BaseCtl {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private IMgmProductCategoryService mgmProductCategoryService;
	@Autowired
	private IDCProjectService dcprojectService;
	
	@RequestMapping("list")
	public String list(ModelMap mm, @RequestParam(required=false)String menuCode){
		mm.put(MENU_CODE, menuCode);
		this.set(MENU_CODE, menuCode);
		return "sys/category/list";	
	}
	
	@RequestMapping(value="list2", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String list2(){

		List<MgmProductCategory> list 	= this.mgmProductCategoryService.queryAll();
		
		List<ZTree> list2 	= new ArrayList<ZTree>();
		ZTree zt = null;
		for(MgmProductCategory m : list){
			zt = new ZTree(m.getId(), m.getGcParentId(), m.getGcName(), m.getId().toString());
			zt.setOpen(true);
			list2.add(zt);
		}
		
		return JSONObject.toJSONString(list2, features);
	
	}
	
	@RequestMapping(value="nextCategory", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String nextCategory(Integer id){
		MgmProductCategory mgmProductCategory = this.mgmProductCategoryService.findById(id);
		List<MgmProductCategory> mlist = this.mgmProductCategoryService.nextCategory(id);
		JSONObject json = new JSONObject();
		json.put("type", mgmProductCategory.getType());
		json.put("twotype", mgmProductCategory.getTwoType());
		json.put("mlist", mlist);
		return JSONObject.toJSONString(json, features);
	
	}
	
	@RequestMapping("transfer")
	public String transfer(Integer id, String action, ModelMap mm){
		MgmProductCategory mgmProductCategory = null;
		if(id != null){
			mgmProductCategory = this.mgmProductCategoryService.findById(id);
			mm.put("mgmProductCategory", mgmProductCategory);
		}
		if("add".equals(action) || "edit".equals(action)){
			mm.put("firstCategory", this.mgmProductCategoryService.firstCategory());
			mm.put("project", this.dcprojectService.queryAll());
		}
		return "sys/category/" + action;
	}
	
	@RequestMapping(value="add", method = RequestMethod.POST)
	@ResponseBody
	public String add(MgmProductCategory mgmProductCategory, String pid, String ptid, String pSid){
		mgmProductCategory.setNum(0);
		mgmProductCategory.setGcParentId(0);
		String mid= "";
		if(mgmProductCategory != null ){
			if(StringUtil.isNotBlank(pid) && !"0".equals(pid)){
				mgmProductCategory.setType(this.mgmProductCategoryService.findById(Integer.valueOf(pid)).getType());
				
				mgmProductCategory.setNum(1);
				mgmProductCategory.setGcParentId(Integer.valueOf(pid));
				
				mid = pid;
				if(StringUtil.isNotBlank(ptid) && !"0".equals(ptid)){
					mgmProductCategory.setNum(2);
					mgmProductCategory.setGcParentId(Integer.valueOf(ptid));
					
					mid = ptid;
					if(StringUtil.isNotBlank(pSid) && !"0".equals(pSid)){
						mgmProductCategory.setNum(3);
						mgmProductCategory.setGcParentId(Integer.valueOf(pSid));
						
						mid = pSid;
					}
				}
				if(mgmProductCategory.getNum().intValue() != 1){
					mgmProductCategory.setTwoType("");
				}
				if(mgmProductCategory.getNum().intValue() == 1 && mgmProductCategory.getType().equals("1")){
					mgmProductCategory.setTwoType("");
				}else{
				}
				if(mgmProductCategory.getNum().intValue() == 1 && mgmProductCategory.getType().equals("2")){
					mgmProductCategory.setProjectId(0);
				}else{
				}
			}
			if(StringUtil.isNotBlank(mid) && mgmProductCategory.getNum().intValue() > 1){
				mgmProductCategory.setTwoType(this.mgmProductCategoryService.findById(Integer.valueOf(mid)).getTwoType());
			}
			if(mgmProductCategory.getGcIcon().endsWith(",")){
				mgmProductCategory.setGcIcon(mgmProductCategory.getGcIcon().substring(0, mgmProductCategory.getGcIcon().length() -1));
			}
			this.mgmProductCategoryService.addMgmProductCategory(mgmProductCategory);
		}
		return AJAX_SUCCESS;
	}

    @RequestMapping(value="edit", method = RequestMethod.POST)
    @ResponseBody
    public String edit(MgmProductCategory mgmProductCategory){
        if(mgmProductCategory != null ){
        	MgmProductCategory mpc = this.mgmProductCategoryService.findById(mgmProductCategory.getId());
        	mpc.setGcName(mgmProductCategory.getGcName());
        	mpc.setGcKeywords(mgmProductCategory.getGcKeywords());
        	mpc.setGcDesc(mgmProductCategory.getGcDesc());
        	mpc.setProjectId(mgmProductCategory.getProjectId());
        	mpc.setGcSort(mgmProductCategory.getGcSort());
        	if(mgmProductCategory.getGcIcon().endsWith(",")){
        		mpc.setGcIcon(mgmProductCategory.getGcIcon().substring(0, mgmProductCategory.getGcIcon().length() -1));
			}
            this.mgmProductCategoryService.updateMgmProductCategory(mgmProductCategory);
        }
        return AJAX_SUCCESS;
    }

	
	
	
	@RequestMapping("del")
	@ResponseBody
	public String del(Integer id){
		if(id != null){
			this.mgmProductCategoryService.delMgmProductCategory(id);
		}
		
		return AJAX_SUCCESS;
	}
	
	
	
		
}

