package com.blueland.sys.app.web;

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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blueland.annotation.Log;
import com.blueland.model.TPfApplication;
import com.blueland.result.R;
import com.blueland.service.IAdminUserService;
import com.blueland.service.IAppDatabaseService;
import com.blueland.service.IApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
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 com.blueland.bo.ApplicationEntity;

import lombok.extern.slf4j.Slf4j;

import static com.blueland.constant.CommonConstant.*;

/**
 * 应用管理
 * @作者 陈琳
 */
@Slf4j
@Controller
@RequestMapping("/application")
public class ApplicationRest {
	@Autowired
	private IApplicationService applicationService;
	
	@Autowired
	private IAdminUserService adminUserService;
	
	@Autowired
	private IAppDatabaseService appDatabaseService;
	
	/**
	 *查询list-包含数据库相关信息，及管理员信息
	 */
	@ResponseBody
	@RequestMapping(value="/list", method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	public Page<ApplicationEntity> listPage(@RequestParam Map<String,Object> queryParams){
    	//queryParams.put("orderByColumns", "T1.create_time");
		Page<ApplicationEntity> page = applicationService.findApplicationPageListByParams(queryParams,Integer.parseInt(queryParams.get("pageNum").toString()),Integer.parseInt(queryParams.get("pageSize").toString()));
		return page;
	}
	
    
    /** 
	 * 搜索对象 
	 **/
	@RequestMapping(value = "",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Page<ApplicationEntity>> list(ApplicationEntity applicationEntity) {
		Page<ApplicationEntity> page = this.applicationService.findPage(applicationEntity);
		return R.ok(page);
	}

	
	/** 
	 * 查看对象
	 **/
	@RequestMapping(value = "/{id}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<TPfApplication> view(@PathVariable String id)  {
		TPfApplication tpfApplicationEntity  = applicationService.getById(id);
		return R.ok(tpfApplicationEntity);
	}


	/** 
	 * 查看对象（支持多条件查询）
	 **/
	@RequestMapping(value = "get",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<TPfApplication> get(ApplicationEntity model)  {
		TPfApplication applicationEntity  = applicationService.get(model);
		return R.ok(applicationEntity);
	}
	
	
	/** 
	 * 新增对象
	 **/
	@Log(busiType = "新增应用", opType = SAVE, type = "web")
	@RequestMapping(value = "",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<ApplicationEntity> save(@RequestBody ApplicationEntity model)  {
		if (ObjectUtil.isEmpty(model.getAppId())) {
			throw new RuntimeException("应用编码不能为空");
		}
		TPfApplication tPfApplication = applicationService.getById(model.getAppId());
		if (ObjectUtil.isNotEmpty(tPfApplication)) {
			throw new RuntimeException("该应用已存在：" + model.getAppId());
		}
		TPfApplication application = BeanUtil.copyProperties(model, TPfApplication.class);
		applicationService.save(application);
		return R.ok(model);
	}
	
	
	/**
	 * 更新对象
	 * @return 
	 **/
	@Log(busiType = "修改应用", opType = UPDATE, type = "web")
	@RequestMapping(value = "",method = RequestMethod.PUT,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Boolean> update(@RequestBody ApplicationEntity model)  {
		TPfApplication application = BeanUtil.copyProperties(model, TPfApplication.class);
		applicationService.updateById(application);
		return R.ok();
	}
	
	/**
	 * 删除对象
	 * @return 
	 **/
	@Log(busiType = "删除应用", opType = DELETE, type = "web")
	@RequestMapping(value = "/{id}",method = RequestMethod.DELETE,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Boolean> delete(@PathVariable String id) {
		applicationService.removeById(id);
		return R.ok();
	}
	
	/**
	 *  批量删除对象
	 * @return 
	 **/
	@Log(busiType = "批量删除应用", opType = DELETE, type = "web")
	@RequestMapping(value = "deleteBatch",method = RequestMethod.DELETE,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Boolean> deleteBatch(String[] ids) {
		for (int i = 0; i < ids.length; i++) {
			applicationService.removeById(ids[i]);
		}
		return R.ok();
	}
	
	/**
	 * 删除应用数据,删除应用数据库相关信息,删除应用管理员信息
	 * @param
	 * @return
	 */
    @ResponseBody 	 	
   	@RequestMapping(value="dealDeleteBatch")
   	public Integer dealDeleteBatch(String[] appIds){
       	if(appIds == null){
       		throw new RuntimeException("id不能为空");
       	}
       	int count = 0;
       	count = applicationService.batchDelete(appIds);
       	count =adminUserService.batchDeleteByAppId(appIds);
       	count +=appDatabaseService.batchDeleteByAppId(appIds);
       	return count;
   	}
	
    
    /**
     * 保存数据
     */
	@Log(busiType = "保存数据", opType = SAVE, type = "web")
    @RequestMapping(value = "/saveDate",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    @CrossOrigin
    public R<Boolean> saveDate(@RequestBody Map<String,Object> params)  {
        Boolean rs = applicationService.saveDate(params);
        return R.ok(rs);
    }
    
    /**
     * 更新数据
     */
	@Log(busiType = "更新数据", opType = UPDATE, type = "web")
    @RequestMapping(value = "/updateDate",method = RequestMethod.PUT,produces = "application/json;charset=UTF-8")
    @ResponseBody
    @CrossOrigin
    public R<Boolean> updateDate(@RequestBody Map<String,Object> params)  {
        Boolean rs = applicationService.updateDate(params);
        return R.ok(rs);
    }

	@RequestMapping(value = "/getList",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<List<ApplicationEntity>> getList(ApplicationEntity model){
		List<ApplicationEntity> list = applicationService.findList(model);
		return R.ok(list);
	}
	
	/**
	 * 查询(应用-模块)组合树
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/getCombinationTreeList",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<List<Map<String, Object>>> getCombinationTreeList(@RequestParam Map<String,Object> params){
		List<Map<String, Object>> list = applicationService.getCombinationTreeList(params);
		return R.ok(list);
	}
	
	@RequestMapping(value = "/getListByUser/{userId}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<List<ApplicationEntity>> getListByUser(@PathVariable String userId){
		List<ApplicationEntity> list= applicationService.findApplicationListByUser(userId);
		return R.ok(list);
	}
}
