package org.springblade.gateway.mall_gateway.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.flow.business.constant.AuditCandidate;
import org.springblade.flow.business.service.FlowBusinessService;
import org.springblade.flow.core.entity.BladeFlow;
import org.springblade.gateway.mall_gateway.entity.vo.AppMallVO;
import org.springblade.gateway.mall_gateway.service.IAppMallService;
import org.springblade.gateway.mall_gateway.wrapper.AppMallWrapper;
import org.springblade.gateway.shop_gateway.entity.vo.MemberInfoVO;
import org.springblade.ldt.activity.service.IActivityService;
import org.springblade.ldt.mall.dto.MallDTO;
import org.springblade.ldt.mall.entity.Mall;
import org.springblade.ldt.mall.service.IMallService;
import org.springblade.ldt.mall.vo.MallVO;
import org.springblade.ldt.shop.entity.Audit;
import org.springblade.ldt.shop.service.IAuditService;
import org.springblade.ldt.user.entity.Member;
import org.springblade.ldt.user.service.IMemberService;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cy-computer
 */
@RestController
@RequestMapping("/mall")
@Api(tags = "商场控制层")
@AllArgsConstructor
public class AppMallController {

	private IAppMallService appMallService;
	private IMemberService memberService;
	private IMallService mallService;
	private IActivityService activityService;
	private IAuditService auditService;
	private FlowBusinessService flowBusinessService;

	@PostMapping("submit")
	@ApiOperation(value = "新建或修改")
	public R submit(@RequestBody @Valid MallDTO mallDTO) {
		return R.data(appMallService.submit(mallDTO));
	}

	@PostMapping("mallAudit")
	@ApiOperation(value = "商场进件")
	public R mallAudit(@RequestBody @Valid Audit audit) {
		return R.status(appMallService.mallAudit(audit));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入mall")
	public R<IPage<AppMallVO>> list(Mall mall, Query query) {
		IPage<Mall> pages = mallService.page(Condition.getPage(query), Condition.getQueryWrapper(mall));
		return R.data(AppMallWrapper.build(activityService).pageVO(pages));
	}


	@GetMapping("statistic")
	@ApiOperation(value = "获取商场统计数据")
	public R statistic(String mallId) {
		return R.data(appMallService.statistic(mallId));
	}

	@GetMapping("memberInfo")
	@ApiOperation(value = "会员中心数据")
	public R memberInfo(@RequestParam(value = "mallId") Long mallId) {

		//会员数量
		int memberNum = memberService.count(Condition.getQueryWrapper(new Member()).lambda().eq(Member::getMallId, mallId));

		Mall mall = mallService.getById(mallId);

		return R.data(MemberInfoVO
			.builder()
			.totalPointValue(mall.getPoint())
			.pointUnitValue(mall.getPointRate())
			.sendPointUnit(mall.getSendPointUnnit())
			.memberNum(memberNum)
			.isOpenMember(mall.getUseMember())
			.build());
	}

	/**
	 * 大b端商场管理，列表
	 */
	@GetMapping("/shopManagementList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "大b端商场管理", notes = "传入mall")
	public R page(MallVO mall, Query query) {
		appMallService.shopManagementList(Condition.getPage(query), mall);
		return R.data(null);
	}

	/**
	 * 下载签约文件
	 */
	@GetMapping("/downloadSign")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "下载签约文件", notes = "传入mall")
	public R downloadSign(MallVO mall) {
		Audit audit = auditService.getOne(new QueryWrapper<Audit>().lambda().eq(Audit::getEntityId, mall.getId()));
		BladeFlow bladeFlow = new BladeFlow();
		bladeFlow.setAssignee(AuditCandidate.$INITIATOR.name());
		bladeFlow.setProcessInstanceId(audit.getProcessInstanceId());
		List<BladeFlow> records = flowBusinessService.selectTodoPage(new Page<BladeFlow>(), bladeFlow).getRecords();
		Assert.isTrue(records.size() > 0, () -> {
			throw new ServiceException("获取进件信息失败！");
		});
		BladeFlow task = records.get(0);
		return R.data(task.getVariables().get("signUrl"));
	}
}
