package com.hotent.portal.controller;


import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.controller.BaseController;
import com.hotent.base.id.IdGenerator;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.util.HttpUtil;
import com.hotent.base.util.StringUtil;
import com.hotent.base.util.time.DateFormatUtil;
import com.hotent.sys.persistence.manager.SysIdentityManager;
import com.hotent.sys.persistence.model.SysIdentity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.*;

/** <pre>
* 描述：流水号 控制器类
* @author dengyg
* @company 广州宏天软件股份有限公司
* @email dengyg@jee-soft.cn
* @date 2018-06-06 14:20
* </pre>
*/
@RestController
@RequestMapping("/sys/identity/v1")
@Api(tags="流水号")
@ApiGroup(group= {ApiGroupConsts.GROUP_PORTAL})
public class SysIdentityController extends BaseController<SysIdentityManager, SysIdentity> {
	@Resource
	SysIdentityManager identityManager;
	@Resource
	protected IdGenerator idGenerator;
	
	@RequestMapping(value="listJson", method= RequestMethod.POST, produces={"application/json; charset=utf-8" })
	@ApiOperation(value = "流水号生成列表(分页条件查询)数据", httpMethod = "POST", notes = "流水号生成列表(分页条件查询)数据")
	public PageList<SysIdentity> listJson(HttpServletRequest request,HttpServletResponse reponse,@ApiParam(name="queryFilter",value="通用查询对象")@RequestBody QueryFilter<SysIdentity> queryFilter) throws Exception{
		return identityManager.query(queryFilter);
	}
	
	@RequestMapping(value="getAll", method= RequestMethod.POST, produces={"application/json; charset=utf-8" })
	@ApiOperation(value = "获取所有流水号", httpMethod = "POST", notes = "流水号生成列表(分页条件查询)数据")
	public List<SysIdentity> getAll() throws Exception{
		return identityManager.list();
	}

	@RequestMapping(value="getById",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据id获取流水号", httpMethod = "GET", notes = "根据id获取流水号")
	public Object getById(HttpServletRequest request,HttpServletResponse response,@ApiParam(name="id",value="流水号定义Id", required = true) @RequestParam String id) throws Exception{
		SysIdentity identity = identityManager.get(id);
		return identity;
	}
	
	@RequestMapping(value="getJson",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "流水号明细页面", httpMethod = "GET", notes = "流水号明细页面")
	public Object get(HttpServletRequest request,HttpServletResponse response,@ApiParam(name="id",value="流水号定义Id", required = true) @RequestParam String id) throws Exception{
		SysIdentity identity=null;
		if(StringUtil.isNotEmpty(id)){
			identity=identityManager.get(id);
		}
		return identity;
	}
	
	@RequestMapping(value="save",method=RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "保存流水号", httpMethod = "POST", notes = "保存流水号")
	public Object save(@ApiParam(name="identity",value="流水号定义", required = true) @RequestBody SysIdentity identity) throws  Exception{
		Object res=identityManager.saveSysIdentity(identity);
		return res;
	}
	
	@RequestMapping(value="remove",method=RequestMethod.DELETE, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "批量删除流水号", httpMethod = "DELETE", notes = "批量删除流水号")
	public Object remove(@ApiParam(name="ids",value="流水号定义Ids", required = true) @RequestParam String ids) throws Exception{
		try {
			String[] aryIds=StringUtil.getStringAryByStr(ids);
			identityManager.removeByIds(Arrays.asList(aryIds));
			return new CommonResult<String>(true,"删除成功",null);
		} catch (Exception e) {
			return new CommonResult<String>(false,"删除失败",null);
		}
	}
	
	@RequestMapping(value="preview",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "流水号预览", httpMethod = "GET", notes = "流水号预览")
	public PageList<SysIdentity> preview(@ApiParam(name="alias",value="流水号别名", required = true) @RequestParam String alias) throws Exception{
		List<SysIdentity> identities = identityManager.getPreviewIden(alias);
		return new PageList<SysIdentity>(identities);
	}
	
	@RequestMapping(value="getNextIdByAlias",method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取下一个流水号", httpMethod = "GET", notes = "获取下一个流水号")
	public  CommonResult<String> getNextIdByAlias(@ApiParam(name="alias",value="流水号别名", required = true) @RequestParam String alias) throws Exception{
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("id", null);
		params.put("alias", alias);
		if(identityManager.isAliasExisted(params)){
			String nextId = identityManager.nextId(alias);
			return new CommonResult<String>(true, "获取流水号成功！", nextId);
		}
		return new CommonResult<String>(false, "获取流水号失败！");
	}

	@RequestMapping(value = "getBatchByAlias", method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
	@ApiOperation(value = "通过别名批量获取流水号", httpMethod = "GET", notes = "批量获取流水号")
	public CommonResult<List<String>> getBatchByAlias(@ApiParam(name = "alias", value = "流水号别名", required = true) @RequestParam String alias,
													  @ApiParam(name = "offset", value = "个数", required = true) @RequestParam Integer offset) {
		if (offset < 0) {
			return new CommonResult<>(false, "获取流水号失败， 个数不能小于0");
		} else if (offset == 0) {
			return new CommonResult<>(true, "获取成功", new ArrayList<>());
		}
		return new CommonResult<>(true, "获取成功", identityManager.nextIds(alias, offset));

	}

	@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 = "identity_"+ DateFormatUtil.format(LocalDateTime.now(), "yyyy_MMdd_HHmm");
		String json = identityManager.export(idList);
		HttpUtil.downLoadFile(request,response,json,"identity.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=identityManager.importIdentity(request,response);
		return res;
	}

    @RequestMapping(value="importData", method=RequestMethod.POST, produces={"application/json; charset=utf-8" })
    @ApiOperation(value = "导入自定义查询", httpMethod = "POST", notes = "导入自定义查询")
    public CommonResult<String> importData(@ApiParam(name="unZipFilePath",value="unZipFilePath", required = true)@RequestParam String unZipFilePath,
                                           @ApiParam(name="status",value="status", required = true)@RequestParam String status) throws Exception{
        CommonResult<String> res=identityManager.importFileData(unZipFilePath,status);
        return res;
    }
}
