package cn.superdesk.uniorder.controller.marketing;

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.marketing.UnifiedStatisticsVirtDimDay;
import cn.superdesk.uniorder.dto.param.marketing.UnifiedStatisticsVirtDimDayParam;
import cn.superdesk.uniorder.dto.param.marketing.UnifiedStatisticsVirtDimDayQueryCriteria;
import cn.superdesk.uniorder.dubbo.consumer.marketing.UnifiedStatisticsVirtDimDayDubboConsumerService;
import cn.superdesk.uniorder.executor.scheduler.task.StatisticsJobVirtOrderStatisticalDimDayTask;
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-09-06 12:00:09
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-09-06 12:00:09
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/unifiedStatisticsVirtDimDay")
public class UnifiedStatisticsVirtDimDayController extends BaseController {

	@Autowired
	private UnifiedStatisticsVirtDimDayDubboConsumerService unifiedStatisticsVirtDimDayDubboConsumerService;

	/****************************************** 手动执行任务 start ******************************************/
	@Autowired
	private StatisticsJobVirtOrderStatisticalDimDayTask statisticsJobVirtOrderStatisticalDimDayTask;

	/**
	 * http://192.168.6.223:8089/web/unifiedStatisticsVirtDimDay/task
	 * {
	 * "year" : 2021,
	 * "month" : 9,
	 * "dayOfMonth" : 6
	 * }
	 *
	 * @param criteria
	 * @return
	 */
	@ApiOperation(value = "手动执行任务-指定日期")
	@RequestMapping(value = "task", method = RequestMethod.POST)
	@ResponseBody
	public R<String> task(@RequestBody UnifiedStatisticsVirtDimDayQueryCriteria criteria) {

		// TODO 参数合法性判定

		log.info("{}{}", "UnifiedStatisticsVirtDimDayController -> task", JsonUtils.toJson(criteria));

		statisticsJobVirtOrderStatisticalDimDayTask.runTask(criteria.getYear(), criteria.getMonth(), criteria.getDayOfMonth());

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

	/****************************************** 手动执行任务   end ******************************************/

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

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

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

		// TODO 参数合法性判定

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

		return result;
	}

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

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

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

		// TODO 参数合法性判定

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

		return result;
	}

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

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

		// TODO 参数合法性判定

		UnifiedStatisticsVirtDimDay entity = unifiedStatisticsVirtDimDayDubboConsumerService.findUnifiedStatisticsVirtDimDayById(id);

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

		result.setData(data);

		return result;
	}

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

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


		// TODO 参数合法性判定

		unifiedStatisticsVirtDimDayDubboConsumerService.addUnifiedStatisticsVirtDimDay(param);

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

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

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

		unifiedStatisticsVirtDimDayDubboConsumerService.updateUnifiedStatisticsVirtDimDay(param);

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

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

		// TODO 参数合法性判定

		unifiedStatisticsVirtDimDayDubboConsumerService.deleteUnifiedStatisticsVirtDimDay(id);

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

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

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

		for (long id : ids) {
			unifiedStatisticsVirtDimDayDubboConsumerService.deleteUnifiedStatisticsVirtDimDay(id);
		}

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

	}


}
