package com.hotent.portal.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotent.base.controller.BaseController;
import com.hotent.base.controller.BaseController2;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.Direction;
import com.hotent.base.query.FieldSort;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.util.*;
import com.hotent.base.util.time.DateFormatUtil;
import com.hotent.portal.model.AppModel;
import com.hotent.portal.params.AppTree;
import com.hotent.portal.persistence.manager.AppModelManager;
import com.hotent.sys.persistence.manager.SysAuthUserManager;
import com.hotent.sys.persistence.manager.SysCategoryManager;
import com.hotent.sys.persistence.manager.SysTypeManager;
import com.hotent.sys.persistence.model.SysCategory;
import com.hotent.sys.persistence.model.SysType;
import com.hotent.uc.api.impl.util.ContextUtil;
import com.hotent.uc.api.model.IUser;
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.*;
import java.util.stream.Collectors;

/**
 * 
 * <pre> 
 * 描述：移动端应用 控制器类
 * 构建组：x7
 * 作者:pangq
 * 邮箱:pangq@jee-soft.cn
 * 日期:2020-07-06 15:25:12
 * 版权：广州宏天软件股份有限公司
 * </pre>
 */
@RestController
@RequestMapping(value="/portal/appModel/v2")
@Api(tags="appModelController2")
public class AppModelController2 extends BaseController2<AppModelManager,AppModel> {
	@Resource
	AppModelManager appModelManager;
	@Resource
	SysTypeManager sysTypeManager;
	@Resource
	SysCategoryManager sysCategoryManager;
	@Resource
	SysAuthUserManager sysAuthUserManager;
	/**
	 * 移动端应用列表(分页条件查询)数据
	 * @param queryFilter
	 * @return
	 * @throws Exception 
	 * PageJson
	 * @exception 
	 */
	@PostMapping("/listJson")
	@ApiOperation(value="移动端应用数据列表", httpMethod = "POST", notes = "获取移动端应用列表")
	public CommonResult<PageList<AppModel>> list(@ApiParam(name="queryFilter",value="查询对象")@RequestBody QueryFilter<AppModel> queryFilter) throws Exception{
		List<FieldSort> sorter = queryFilter.getSorter();
		sorter.add(new FieldSort("ID_", Direction.DESC));
		queryFilter.setSorter(sorter);
		PageList<AppModel> pageList = appModelManager.query(queryFilter);
		return CommonResult.<PageList<AppModel>>ok().value(pageList);
	}
	
	@RequestMapping(value="getApp", method=RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据可以key获取App", httpMethod = "GET", notes = "根据可以key获取App")
	public CommonResult<List<AppTree>> getApp(@ApiParam(name="typeKey", value="分类key", required = true)@RequestParam String typeKey,
											  @ApiParam(name="appName", value="appName", required = false)@RequestParam String appName) throws Exception {
		List<AppTree> appTreeList = appModelManager.getApp(typeKey,appName);
		return CommonResult.<List<AppTree>>ok().value(appTreeList);
	}

	
	/**
	 * 移动端应用明细页面
	 * @param id
	 * @return
	 * @throws Exception 
	 * ModelAndView
	 */
	@GetMapping(value="/getJson")
	@ApiOperation(value="移动端应用数据详情",httpMethod = "GET",notes = "移动端应用数据详情")
	public CommonResult<AppModel> get(@ApiParam(name="id",value="业务对象主键", required = true)@RequestParam String id) throws Exception{
		AppModel appModel = appModelManager.get(id);
		return CommonResult.<AppModel>ok().value(appModel);
	}
	
    /**
	 * 新增移动端应用
	 * @param appModel
	 * @throws Exception 
	 * @return
	 * @exception 
	 */
	@PostMapping(value="save")
	@ApiOperation(value = "新增,更新移动端应用数据", httpMethod = "POST", notes = "新增,更新移动端应用数据")
	public CommonResult<String> save(@ApiParam(name="appModel",value="移动端应用业务对象", required = true)@RequestBody AppModel appModel) throws Exception{
		String msg = "添加移动端应用成功";
		IUser user = ContextUtil.getCurrentUser();
		if(StringUtil.isEmpty(appModel.getId())){
			appModel.setCreateBy(user.getUserId());
			appModel.setCreateTime(LocalDateTime.now());
			appModelManager.create(appModel);
		}else{
			appModel.setUpdateBy(user.getUserId());
			appModel.setUpdateTime(LocalDateTime.now());
			appModelManager.update(appModel);
			 msg = "更新移动端应用成功";
		}
		return CommonResult.<String>ok().message(msg);
	}

	@GetMapping(value="updateEnable/{id}")
	@ApiOperation(value = "更改应用中心状态", httpMethod = "POST", notes = "更改应用中心状态")
	public CommonResult<String> updateEnable(@PathVariable("id") String id){
		AppModel appModel = appModelManager.get(id);
		if (BeanUtils.isEmpty(appModel)){
			return CommonResult.<String>error().message("根据ID无法查询到应用中心");
		}
		if (appModel.getEnable().equals(0)){
			appModel.setEnable(1);
		}else{
			appModel.setEnable(0);
		}
		appModelManager.update(appModel);
		return CommonResult.<String>ok().message("状态修改成功");
	}


	@RequestMapping(value="exportXml" ,method= RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "导出移动端应用xml", httpMethod = "GET", notes = "导出移动端应用xml")
	public void exportXml(HttpServletRequest request, HttpServletResponse response,
						  @ApiParam(name="ids",value="移动端应用id", required = true) @RequestParam String ids) throws Exception {
		response.setContentType("APPLICATION/OCTET-STREAM");
		if (BeanUtils.isNotEmpty(ids)) {
			String[] stringsIds = ids.split(",");
			List<String> list = Arrays.asList(stringsIds);
			String zipName = "ht_appModel_"+ DateFormatUtil.format(LocalDateTime.now(), "yyyy_MMdd_HHmmss");
			// 写XML
			Map<String, String> strXml = baseService.exportData(list);
			HttpUtil.downLoadFile(request, response, strXml,zipName);
		}
	}

	@RequestMapping(value = "import", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
	@ApiOperation(value = "移动端应用导入", httpMethod = "POST", notes = "移动端应用导入")
	public CommonResult<String> importData(MultipartHttpServletRequest request, HttpServletResponse response) throws Exception {
		CommonResult<String> commonResult = appModelManager.importData(request, response);
		return CommonResultUtil.<String>getResult(commonResult);
	}
}
