/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.distribution.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.Json;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.comment.model.DistributionLevelSet;
import com.yami.shop.distribution.comment.service.DistributionLevelService;
import com.yami.shop.distribution.comment.service.DistributionLevelSetService;
import com.yami.shop.security.platform.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 *
 * @author lgh on 2019/04/04.
 */
@RestController
@RequestMapping("/distribution/distributionLevelSet")
@AllArgsConstructor
public class DistributionLevelSetController {

    private DistributionLevelSetService distributionLevelSetService;

    private DistributionLevelService distributionLevelService;

	/**
	 * 分页获取
	 */
    @GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('distribution:distributionLevelSet:page')")
	public ResponseEntity<IPage<DistributionLevelSet>> page(DistributionLevelSet distributionLevelSet, PageParam<DistributionLevelSet> page){
		IPage<DistributionLevelSet> list = distributionLevelSetService.page(page,new LambdaQueryWrapper<DistributionLevelSet>());
		return ResponseEntity.ok(list);
	}

	/**
	 * 获取信息(根据店铺)
	 */
	@GetMapping("/info")
	@PreAuthorize("@pms.hasPermission('distribution:distributionLevelSet:info')")
	public ResponseEntity<DistributionLevelSet> info(){
		DistributionLevelSet distributionLevelSet = distributionLevelSetService.getByShopId();
		return ResponseEntity.ok(distributionLevelSet);
	}

	/**
	 * 保存
	 */
	@PostMapping
	@PreAuthorize("@pms.hasPermission('distribution:distributionLevelSet:save')")
	public ResponseEntity<Void> save(@RequestBody @Valid DistributionLevelSet distributionLevelSet){
		distributionLevelSetService.saveByDistributionLevelSet(distributionLevelSet);
		return ResponseEntity.ok().build();
	}

	/**
	 * 修改
	 */
	@PutMapping
	@PreAuthorize("@pms.hasPermission('distribution:distributionLevelSet:update')")
	public ResponseEntity<Void> update(@RequestBody @Valid DistributionLevelSet distributionLevelSet){
		distributionLevelSet.setLevelSetConditionsSwitchJson(Json.toJsonString(distributionLevelSet.getLevelSetConditionsSwitch()));
		distributionLevelSet.setUpdateTime(new Date());
		distributionLevelSet.setShopId(Constant.PLATFORM_SHOP_ID);
		distributionLevelSet.setModifier(SecurityUtils.getSysUser().getUserId());
		distributionLevelSetService.updateByDistributionLevelSet(distributionLevelSet);
		return ResponseEntity.ok().build();
	}

	/**
	 * 删除
	 */
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('distribution:distributionLevelSet:delete')")
	public ResponseEntity<Void> delete(@PathVariable Long id){
		distributionLevelSetService.removeById(id);
		return ResponseEntity.ok().build();
	}
}
