package cn.superdesk.uniorder.controller.organisation;

import cn.superdesk.libs.common.json.JsonUtils;
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.organisation.SysCliqueOrg;
import cn.superdesk.uniorder.dto.param.organisation.SysCliqueOrgParam;
import cn.superdesk.uniorder.dubbo.consumer.organisation.SysCliqueOrgDubboConsumerService;
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-04-28 14:08:25
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-04-28 14:08:25
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "集团分公司X项目企业-集团分公司(数据来源-服务之窗)", tags = {"集团分公司X项目企业-集团分公司(数据来源-服务之窗)"})
@Slf4j
@RestController
@RequestMapping("/sysCliqueOrg")
public class SysCliqueOrgController extends BaseController {

	@Autowired
	private SysCliqueOrgDubboConsumerService sysCliqueOrgDubboConsumerService;

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

		log.info("pageQuerySysCliqueOrg:\n{}", JsonUtils.toJson(param));

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

		// TODO 参数合法性判定

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

		return result;
	}

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

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

		// TODO 参数合法性判定

		SysCliqueOrg entity = sysCliqueOrgDubboConsumerService.findSysCliqueOrgById(id);

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

		result.setData(data);

		return result;
	}

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

		log.info("addSysCliqueOrg:\n{}", JsonUtils.toJson(param));


		// TODO 参数合法性判定

		sysCliqueOrgDubboConsumerService.addSysCliqueOrg(param);

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

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

		log.info("updateSysCliqueOrg:\n{}", JsonUtils.toJson(param));

		sysCliqueOrgDubboConsumerService.updateSysCliqueOrg(param);

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

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

		// TODO 参数合法性判定

		sysCliqueOrgDubboConsumerService.deleteSysCliqueOrg(id);

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

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

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

		for (long id : ids) {
			sysCliqueOrgDubboConsumerService.deleteSysCliqueOrg(id);
		}

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

	}


}
