package cn.superdesk.uniorder.controller.operation;

import cn.superdesk.libs.common.json.JsonUtils;
import cn.superdesk.uniorder.common.pagination.PageQueryGenericParam;
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.operation.ExcelImportLog;
import cn.superdesk.uniorder.dto.param.operation.ExcelImportLogParam;
import cn.superdesk.uniorder.dto.param.operation.ExcelImportLogQueryCriteria;
import cn.superdesk.uniorder.dubbo.consumer.operation.ExcelImportLogDubboConsumerService;
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: Excel导入日志
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2021-08-23 10:38:52
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-08-23 10:38:52
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/excelImportLog")
public class ExcelImportLogController extends BaseController {

	@Autowired
	private ExcelImportLogDubboConsumerService excelImportLogDubboConsumerService;

	@ApiOperation(value = "分页查询-默认不用")
	@RequestMapping(value = "listByObscure", method = RequestMethod.POST)
	@ResponseBody
	public R<List<ExcelImportLog>> pageQueryExcelImportLog(@RequestBody PageQueryParam param) {

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

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

		// TODO 参数合法性判定

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

		return result;
	}

	@ApiOperation(value = "分页查询")
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public R<List<ExcelImportLog>> pageQueryGenericParamExcelImportLog(@RequestBody PageQueryGenericParam<ExcelImportLogQueryCriteria> param) {

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

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

		// TODO 参数合法性判定

		result = excelImportLogDubboConsumerService.pageQueryGenericParam(param.getConditions());

		return result;
	}

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

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

		// TODO 参数合法性判定

		ExcelImportLog entity = excelImportLogDubboConsumerService.findExcelImportLogById(id);

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

		result.setData(data);

		return result;
	}

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

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


		// TODO 参数合法性判定

		excelImportLogDubboConsumerService.addExcelImportLog(param);

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

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

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

		excelImportLogDubboConsumerService.updateExcelImportLog(param);

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

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

		// TODO 参数合法性判定

		excelImportLogDubboConsumerService.deleteExcelImportLog(id);

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

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

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

		for (long id : ids) {
			excelImportLogDubboConsumerService.deleteExcelImportLog(id);
		}

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

	}


}
