package cn.superdesk.uniorder.controller.sys.notify;

import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.pagination.PageQueryParam;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.sys.notify.SysNotifyTemplate;
import cn.superdesk.uniorder.dto.param.system.notify.SysNotifyTemplateParam;
import cn.superdesk.uniorder.dubbo.consumer.sys.notify.SysNotifyTemplateDubboConsumerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description:
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2021-03-01 17:37:17
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-03-01 17:37:17
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "系统管理-通知-模板管理", tags = {"系统管理-通知-模板管理"})
@Slf4j
@RestController
@RequestMapping("/sysNotifyTemplate")
public class SysNotifyTemplateController extends BaseController {

	@Autowired
	private SysNotifyTemplateDubboConsumerService sysNotifyTemplateDubboConsumerService;

	@ApiOperation(value = "分页查询")
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public R<List<SysNotifyTemplate>> pageQuerySysNotifyTemplates(@RequestBody PageQueryParam param) {

		log.info("pageQuerySysNotifyTemplate:\n{}", AnJsonUtils.toJson(param));

		R<List<SysNotifyTemplate>> result = new R<>();

		// TODO 参数合法性判定

		result = sysNotifyTemplateDubboConsumerService.pageQuery(param.getConditions(), param.getPageNo(), param.getPageSize());

		return result;
	}

	@ApiOperation(value = "按id查询")
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	@ResponseBody
	public R<List<SysNotifyTemplate>> getById(@PathVariable("id") long id) {

		R<List<SysNotifyTemplate>> result = new R<>();

		// TODO 参数合法性判定

		SysNotifyTemplate entity = sysNotifyTemplateDubboConsumerService.findSysNotifyTemplateById(id);

		List<SysNotifyTemplate> data = new ArrayList<>();
		data.add(entity);

		result.setData(data);

		return result;
	}

	@ApiOperation(value = "新增")
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public R<String> addSysNotifyTemplate(@RequestBody SysNotifyTemplateParam param) {

		log.info("addSysNotifyTemplate:\n{}", AnJsonUtils.toJson(param));


		// TODO 参数合法性判定

		sysNotifyTemplateDubboConsumerService.addSysNotifyTemplate(param);

		return new R<>("200", "操作成功", true);
	}

	@ApiOperation(value = "更新")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public R<String> updateSysNotifyTemplate(@RequestBody SysNotifyTemplateParam param) {

		log.info("updateSysNotifyTemplate:\n{}", AnJsonUtils.toJson(param));

		sysNotifyTemplateDubboConsumerService.updateSysNotifyTemplate(param);

		return new R<>("200", "操作成功", true);
	}

	@ApiOperation(value = "删除")
	@RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public R<String> deleteSysNotifyTemplate(@PathVariable("id") long id) {

		// TODO 参数合法性判定

		sysNotifyTemplateDubboConsumerService.deleteSysNotifyTemplate(id);

		return new R<>("200", "操作成功", true);
	}

	@ApiOperation(value = "批量删除")
	@RequestMapping(value = "delete_ids", method = RequestMethod.POST)
	@ResponseBody
	public R<String> deleteIdsSysNotifyTemplate(@RequestParam(value = "ids") long[] ids) {

		log.info(AnJsonUtils.toJson(ids));

		for (long id : ids) {
			sysNotifyTemplateDubboConsumerService.deleteSysNotifyTemplate(id);
		}

		return new R<>("200", "操作成功", true);

	}


}
