/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.shopping.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.shopping.entity.ConsignConfig;
import org.springblade.modules.shopping.entity.Goods;
import org.springblade.modules.shopping.mapper.ConsignMapper;
import org.springblade.modules.shopping.service.IConsignConfigService;
import org.springblade.modules.shopping.service.IGoodsService;
import org.springblade.modules.shopping.vo.ConsignListVO;
import org.springblade.modules.shopping.wrapper.ConsignWrapper;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.shopping.entity.Consign;
import org.springblade.modules.shopping.vo.ConsignVO;
import org.springblade.modules.shopping.service.IConsignService;
import org.springblade.core.boot.ctrl.BladeController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品寄售表 控制器
 *
 * @author BladeX
 * @since 2022-02-13
 */
@RestController
@AllArgsConstructor
@RequestMapping("shopping/consign")
@Api(value = "商品寄售表", tags = "商品寄售表接口")
public class ConsignController extends BladeController {

	private final IConsignService consignService;
	private final IConsignConfigService consignConfigService;
	private final IGoodsService goodsService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入consign")
	public R<Consign> detail(Consign consign) {
		Consign detail = consignService.getOne(Condition.getQueryWrapper(consign));
		return R.data(detail);
	}

	/**
	 * 分页 商品寄售表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入consign")
	public R<IPage<ConsignVO>> list(Consign consign, Query query) {
		IPage<Consign> pages = consignService.page(Condition.getPage(query),
			Condition.getQueryWrapper(consign).orderByDesc("create_time"));
		return R.data(ConsignWrapper.build().pageVO(pages));
	}

	/**
	 * 分页 查询平台兜底的寄售订单
	 */
	@GetMapping("/platform_list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入consign")
	public R<IPage<ConsignVO>> platformList(Consign consign, Query query) {
		consign.setConsignState(2); //待付款
		consign.setConsignFor(2); //用户订单
		IPage<Consign> pages = consignService.page(Condition.getPage(query),
			Condition.getQueryWrapper(consign)
				.eq("now_stock", 0)
				.orderByDesc("create_time"));
		return R.data(ConsignWrapper.build().pageVO(pages));
	}

	/**
	 * 自定义分页 商品寄售表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入consign")
	public R<IPage<ConsignVO>> page(ConsignVO consign, Query query) {
		IPage<ConsignVO> pages = consignService.selectConsignPage(Condition.getPage(query), consign);
		return R.data(pages);
	}

	/**
	 * 新增 商品寄售表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入consign")
	public R save(@Valid @RequestBody ConsignVO consign) {
		if (consign.getProductNo() != null){
			Goods goods = goodsService.getOne(Wrappers.<Goods>lambdaQuery().eq(Goods::getProductNo, consign.getProductNo()));
			if (goods != null) {
				consign.setGoodsId(goods.getId());
				consign.setGoodsName(goods.getGoodsName());
			}
		}
		if (consign.getConsignConfigId() != null){
			ConsignConfig consignConfig = consignConfigService.getById(consign.getConsignConfigId());
			if (consignConfig != null){
				String ymd = DateUtil.format(DateUtil.now(), DateUtil.PATTERN_DATE);
				String st = DateUtil.format(consignConfig.getStartTime(), DateUtil.PATTERN_TIME);
				String et = DateUtil.format(consignConfig.getEndTime(), DateUtil.PATTERN_TIME);
				String startTime = ymd + " " + st;
				String endTime = ymd + " " + et;
				Date startDate = DateUtil.parse(startTime, DateUtil.PATTERN_DATETIME);
				Date endDate = DateUtil.parse(endTime, DateUtil.PATTERN_DATETIME);
				consign.setStartTime(startDate);
				consign.setEndTime(endDate);
			}
		}
		consign.setConsignUserId(0L);//0标识系统新增的
		consign.setConsignState(1);//寄售中/待抢购
		consign.setConsignFor(1);//寄售来源为商品
		consign.setConsignDays(0);//寄售天数为0,表示没有限制
		consign.setConsignType(0);//无
		consign.setNowStock(consign.getTotalNum());//开始库存一样
		return R.status(consignService.save(consign));
	}

	/**
	 * 修改 商品寄售表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入consign")
	public R update(@Valid @RequestBody ConsignVO consign) {
		if (consign.getProductNo() != null){
			Goods goods = goodsService.getOne(Wrappers.<Goods>lambdaQuery().eq(Goods::getProductNo, consign.getProductNo()));
			if (goods != null) {
				consign.setGoodsId(goods.getId());
				consign.setGoodsName(goods.getGoodsName());
			}
		}
		if (consign.getConsignConfigId() != null){
			ConsignConfig consignConfig = consignConfigService.getById(consign.getConsignConfigId());
			if (consignConfig != null){
				consign.setStartTime(consignConfig.getStartTime());
				consign.setEndTime(consignConfig.getEndTime());
			}
		}
		return R.status(consignService.updateById(consign));
	}

	/**
	 * 新增或修改 商品寄售表
	 */
	@ApiLog("新增修改寄售商品")
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入consign")
	public R submit(@Valid @RequestBody ConsignVO consign) {

		return R.status(consignService.saveOrUpdate(consign));
	}


	/**
	 * 删除 商品寄售表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(consignService.removeByIds(Func.toLongList(ids)));
	}


}
