package com.hotent.portal.controller;


import java.time.LocalDateTime;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.web.bind.annotation.RequestBody;
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.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.cache.annotation.CacheEvict;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.controller.BaseController;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.constants.CacheKeyConst;
import com.hotent.base.util.HttpUtil;
import com.hotent.base.util.StringUtil;
import com.hotent.base.util.UniqueIdUtil;
import com.hotent.base.util.time.DateFormatUtil;
import com.hotent.sys.persistence.manager.SysSignetManager;
import com.hotent.sys.persistence.model.SysSignet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 * 
 * <pre> 
 * 描述：印章管理 控制器类
 * 构建组：x7
 * 作者:zhangxw
 * 邮箱:zhangxw@jee-soft.cn
 * 日期:2020-12-25 17:29:59
 * 版权：广州宏天软件有限公司
 * </pre>
 */
@RestController
@RequestMapping("/sys/sysSignet/v1")
@Api(tags="印章管理")
@ApiGroup(group= {ApiGroupConsts.GROUP_PORTAL})
public class SysSignetController extends BaseController<SysSignetManager, SysSignet> {
	@Resource
	SysSignetManager sysSignetManager;
	

	@RequestMapping(value="listJson", method=RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "印章列表", httpMethod = "POST", notes = "印章列表")
	public PageList<SysSignet> listJson(@ApiParam(name="queryFilter",value="通用查询对象")@RequestBody QueryFilter<SysSignet> queryFilter) throws Exception {
		return sysSignetManager.query(queryFilter);
	}
	
	@RequestMapping(value="getJson", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "印章明细页面", httpMethod = "GET", notes = "印章明细页面")
	public @ResponseBody SysSignet getJson(@ApiParam(name="id", value="主键", required = true)@RequestParam String id) throws Exception {
		SysSignet sysSignet = new SysSignet();
		if(StringUtil.isNotEmpty(id)) {
			sysSignet = sysSignetManager.get(id);
		}
		return sysSignet;
	}
	
	@RequestMapping(value="save", method=RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "保存印章信息", httpMethod = "POST", notes = "保存印章信息")
	public CommonResult<String> save(@ApiParam(name="sysSignet", value="印章", required = true)@RequestBody @Valid SysSignet sysSignet) throws Exception {
		String resultMsg = "";
		if(StringUtil.isEmpty(sysSignet.getId())){
			if(BeanUtils.isNotEmpty(sysSignetManager.getByAlias(sysSignet.getAlias()))) {
				return new CommonResult<String>(false, "别名【"+sysSignet.getAlias()+"】已存在，请输入其他别名。");
			}
			sysSignet.setId(UniqueIdUtil.getSuid());
			sysSignetManager.create(sysSignet);
			resultMsg="添加印章成功";
		}else{
			sysSignetManager.update(sysSignet);
			resultMsg="更新印章成功";
		}
		return new CommonResult<String>(true, resultMsg);
	}

	@RequestMapping(value="remove", method=RequestMethod.DELETE, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "批量删除印章记录", httpMethod = "DELETE", notes = "批量删除印章记录1,2,3")
	@CacheEvict(value = CacheKeyConst.EIP_SYS_PROPS, allEntries = true)
	public CommonResult<String> remove(@ApiParam(name="id", value="主键", required = true)@RequestParam String ids) throws Exception {
		String[] aryIds=StringUtil.getStringAryByStr(ids);
		sysSignetManager.removeByIds(aryIds);
		return new CommonResult<String>(true, "删除印章成功");
	}
	
	@RequestMapping(value="getByAlias", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据别名获取印章信息。", httpMethod = "GET", notes = "根据别名获取印章信息")
	public SysSignet getByAlias(@ApiParam(name="alias", value="印章别名", required = true)@RequestParam String alias) throws Exception {
		return sysSignetManager.getByAlias(alias);
	}
	
	@RequestMapping(value="export",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "导出数据", httpMethod = "GET", notes = "导出数据")
	public void export(HttpServletResponse response,HttpServletRequest request,
					   @ApiParam(name="ids",value="ids", required = true)@RequestParam String ids) throws Exception{
		String[] idList = ids.split(",");
		String fileName = "signet_"+ DateFormatUtil.format(LocalDateTime.now(), "yyyy_MMdd_HHmm");
		String json = sysSignetManager.export(idList);
		HttpUtil.downLoadFile(request,response,json,"signet.json",fileName);
	}

	@RequestMapping(value="import", method=RequestMethod.POST, produces={"application/json; charset=utf-8" })
	@ApiOperation(value = "导入印章", httpMethod = "POST", notes = "导入印章")
	public CommonResult<String> importIdentity(MultipartHttpServletRequest request, HttpServletResponse response) throws Exception{
		CommonResult<String> res=sysSignetManager.importIdentity(request,response);
		return res;
	}

	@RequestMapping(value="confirmImport", method=RequestMethod.POST, produces={"application/json; charset=utf-8" })
	@ApiOperation(value = "导入印章", httpMethod = "POST", notes = "确认导入印章的行为")
	public CommonResult<String> confirmImportIdentity(@RequestParam String unZipFilePath, @RequestParam String status) throws Exception{
		CommonResult<String> res=sysSignetManager.confirmImportIdentity(unZipFilePath,status);
		return res;
	}

}
