package com.gic.haoban.manage.web.controller;

import com.gic.haoban.common.utils.HaobanResponse;
import com.gic.haoban.manage.api.dto.ApplicationEnterpriseRelatedDTO;
import com.gic.haoban.manage.api.dto.ApplicationTemplateDTO;
import com.gic.haoban.manage.api.dto.TemplateEnterpriseRelatedDTO;
import com.gic.haoban.manage.api.service.ApplicationTemplateApiService;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ApplicationTemplateController extends WebBaseController{
	
	@Autowired
	private ApplicationTemplateApiService applicationTemplateApiService;
	
	
	@RequestMapping("list-application-template")
	public HaobanResponse listTemplate(String applicationTemplateName){
		List<ApplicationTemplateDTO> templateList = applicationTemplateApiService.listByTemplateName(applicationTemplateName);
		return resultResponse(HaoBanErrCode.ERR_1,templateList);
		
	}
	
	@RequestMapping("add-template")
	public HaobanResponse addTemplate(String applicationTemplateName,String applicationIds){
		ApplicationTemplateDTO template = applicationTemplateApiService.selectByTemplateName(applicationTemplateName);
		if(template != null){
			return resultResponse(HaoBanErrCode.ERR_10020);
		}
		template = new ApplicationTemplateDTO();
		template.setApplicationTemplateName(applicationTemplateName);
		applicationTemplateApiService.insertTemplate(template,applicationIds);
		return resultResponse(HaoBanErrCode.ERR_1);
		
	}
	
	
	@RequestMapping("del-template")
	public HaobanResponse delTemplate(String applicationTemplateId){
		List<TemplateEnterpriseRelatedDTO> list = applicationTemplateApiService.listEnterpriseByTemplateId(applicationTemplateId);
		if(list != null && !list.isEmpty()){
			return resultResponse(HaoBanErrCode.ERR_10020);
		}
		applicationTemplateApiService.delTemplate(applicationTemplateId);
		return resultResponse(HaoBanErrCode.ERR_1);
		
	}
	
	@RequestMapping("edit-template")
	public HaobanResponse editTemplate(String applicationTemplateId,String applicationTemplateName,String applicationIds){
		ApplicationTemplateDTO template = applicationTemplateApiService.selectByTemplateName(applicationTemplateName);
		if(template != null && !template.getApplicationTemplateId().equals(applicationTemplateId)){
			return resultResponse(HaoBanErrCode.ERR_10020);
		}
		applicationTemplateApiService.editTemplate(applicationTemplateId,applicationTemplateName,applicationIds);
		return resultResponse(HaoBanErrCode.ERR_1);
		
	}
	
	@RequestMapping("add-template-enterprise")
	public HaobanResponse addTemplateEnterprise(String applicationTemplateId,String wxEnterpriseIds){
		applicationTemplateApiService.addTemplateEnterprise(applicationTemplateId,wxEnterpriseIds);
		return resultResponse(HaoBanErrCode.ERR_1);
		
	}
	
	@RequestMapping("del-template-enterprise")
	public HaobanResponse delTemplateEnterprise(String wxEnterpriseId){
		applicationTemplateApiService.delTemplateEnterprise(wxEnterpriseId);
		return resultResponse(HaoBanErrCode.ERR_1);
		
	}
	
	@RequestMapping("fresh-template-enterprise")
	public HaobanResponse freshTemplateEnterprise(String applicationTemplateId){
		//applicationTemplateApiService.freshTemplateEnterprise(applicationTemplateId);
		return resultResponse(HaoBanErrCode.ERR_1);
		
	}
	
	@RequestMapping("view-template-enterprise")
	public HaobanResponse viewTemplateEnterprise(String applicationTemplateId,String wxEnterpriseName,String enterpriseName){
		List<ApplicationEnterpriseRelatedDTO> list = applicationTemplateApiService.listTemplateEnterprise(applicationTemplateId,wxEnterpriseName,enterpriseName);
		return resultResponse(HaoBanErrCode.ERR_1,list);
		
	}
	
	@RequestMapping("set-default-template")
	public HaobanResponse setDefaultTemplateEnterprise(String applicationTemplateId){
		applicationTemplateApiService.setDefaultTemplateEnterprise(applicationTemplateId);
		return resultResponse(HaoBanErrCode.ERR_1);
		
	}

}
