/*
 *      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.ldt.mallshoplabel.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.ldt.gxlabel.entity.GxLabel;
import org.springblade.modules.ldt.gxlabel.service.IGxLabelService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.ldt.mallshoplabel.entity.MallShopLabel;
import org.springblade.modules.ldt.mallshoplabel.vo.MallShopLabelVO;
import org.springblade.modules.ldt.mallshoplabel.service.IMallShopLabelService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;
import java.util.stream.Collectors;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2021-08-09
 */
@RestController
@AllArgsConstructor
@RequestMapping("cyzh-ldt/mallshoplabel")
@Api(value = "", tags = "接口")
public class MallShopLabelController extends BladeController {

	private final IMallShopLabelService mallShopLabelService;
	private final IGxLabelService gxLabelService;

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

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入mallShopLabel")
	public R<IPage<MallShopLabel>> list(MallShopLabel mallShopLabel, Query query) {
		IPage<MallShopLabel> pages = mallShopLabelService.page(Condition.getPage(query), Condition.getQueryWrapper(mallShopLabel));
		return R.data(pages);
	}

	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入mallShopLabel")
	public R<IPage<MallShopLabelVO>> page(MallShopLabelVO mallShopLabel, Query query) {
		IPage<MallShopLabelVO> pages = mallShopLabelService.selectMallShopLabelPage(Condition.getPage(query), mallShopLabel);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入mallShopLabel")
	public R save(@Valid @RequestBody MallShopLabel mallShopLabel) {
		return R.status(mallShopLabelService.save(mallShopLabel));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入mallShopLabel")
	public R update(@Valid @RequestBody MallShopLabel mallShopLabel) {
		return R.status(mallShopLabelService.updateById(mallShopLabel));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入mallShopLabel")
	public R submit(@Valid @RequestBody MallShopLabel mallShopLabel) {
		return R.status(mallShopLabelService.saveOrUpdate(mallShopLabel));
	}


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

	/**
	 * 商场设置行业种类
	 */
	@PostMapping("/mall/setLabels")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入mallShopLabel")
	@Transactional
	public R setLabels(@Valid @RequestBody MallShopLabelVO mallShopLabel) {

		Assert.notNull(mallShopLabel.getMallId(),"商场id不能为空");
		Assert.notNull(mallShopLabel.getLabelIds(),"行业种类不能为空");
		String[] labels = mallShopLabel.getLabelIds().split(",");
		//删除原来的记录
		List<Long> mallShopLabels = mallShopLabelService.getBaseMapper().selectList(new QueryWrapper<>(new MallShopLabel()).lambda()
			.eq(MallShopLabel::getMallId, mallShopLabel.getMallId())).stream().map(MallShopLabel::getId).collect(Collectors.toList());
		mallShopLabelService.removeByIds(mallShopLabels);
		for(String labelId : labels){
			GxLabel gxLabel = gxLabelService.getById(Long.valueOf(labelId));
			if(gxLabel != null){
				MallShopLabel mallShopLabel1 = new MallShopLabel();
				mallShopLabel1.setMallId(mallShopLabel.getMallId());
				mallShopLabel1.setName(gxLabel.getName());
				mallShopLabel1.setStatement(gxLabel.getIntroduce());
				mallShopLabel1.setLabelId(gxLabel.getId());
				mallShopLabelService.save(mallShopLabel1);
			}
		}
		return R.status(true);
	}


}
