/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.cqcis.com</a>
 */
package com.crunii.micro.service.sysmgr.controller;

import cn.hutool.core.date.DateUtil;
import com.crunii.micro.common.dto.PageReq;
import com.crunii.micro.common.dto.PageRsp;
import com.crunii.micro.common.dto.Result;
import com.crunii.micro.domain.entity.main.TsmProperty;
import com.crunii.micro.domain.entity.main.TsmPropertyValue;
import com.crunii.micro.service.sysmgr.dto.sysmgr.*;
import com.crunii.micro.service.sysmgr.service.PropertyService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
* @author 田平 create 2023-02-21 10:48:20
 */
@Api(value = "活动2.0")
@Controller
@Slf4j
@RequestMapping("/auth/PropertyController")
public class PropertyController {
	@Autowired
	private PropertyService propertyService;

	@ApiOperation(value = "属性分页")
	@RequestMapping(path = "/pageProperty", method = RequestMethod.POST)
	@ResponseBody
	public Result<PageRsp<TsmProperty>> pageProperty(@RequestBody @ApiParam PageReq<PropertyReq> req) {
		log.info("req:" + req.toString());
		Result<PageRsp<TsmProperty>> result = new Result<>();
		result.setData(propertyService.pageProperty(req));
		return result;
	}

	@ApiOperation(value = "属性值分页")
	@RequestMapping(path = "/pageValue", method = RequestMethod.POST)
	@ResponseBody
	public Result<PageRsp<TsmPropertyValue>> pageValue(@RequestBody @ApiParam PageReq<ValueReq> req) {
		log.info("req:" + req.toString());
		Result<PageRsp<TsmPropertyValue>> result = new Result<>();
		result.setData(propertyService.pageValue(req));
		return result;
	}

	@ApiOperation(value = "创建属性")
	@RequestMapping(path = "/createProperty", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> createProperty(@RequestBody @ApiParam PropertyC propertyC) {
		Result<String> result = new Result<String>();
		propertyService.createProperty(propertyC);
		return result;
	}

	@ApiOperation(value = "更新属性")
	@RequestMapping(path = "/updateProperty", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> updateProperty(@RequestBody @ApiParam PropertyU propertyU) {
		Result<String> result = new Result<String>();
		propertyService.updateProperty(propertyU);
		return result;
	}

	@ApiOperation(value = "创建属性值")
	@RequestMapping(path = "/createValue", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> createValue(@RequestBody @ApiParam ValueC valueC) {
		Result<String> result = new Result<String>();
		propertyService.createValue(valueC);
		return result;
	}

	@ApiOperation(value = "更新属性值")
	@RequestMapping(path = "/updateValue", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> updateValue(@RequestBody @ApiParam ValueU valueU) {
		Result<String> result = new Result<String>();
		propertyService.updateValue(valueU);
		return result;
	}

	@ApiOperation(value = "软删除属性值")
	@RequestMapping(path = "/delValue", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> delValue(@RequestParam("propCode") String propCode, @RequestParam("valueId") Integer valueId){
		propertyService.deleteValue(propCode, valueId);
		return Result.ofSuccess();
	}

	@ApiOperation(value = "通过属性编码获取属性值列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "propCode", value = "属性编码", required = true)
	})
	@RequestMapping(path = "/getValuesByCode", method = RequestMethod.GET)
	@ResponseBody
	public Result<List<TsmPropertyValue>> getValuesByCode(@RequestParam("propCode") String propCode) {
		Result<List<TsmPropertyValue>> result = new Result<>();
		result.setData(propertyService.getValuesByCode(propCode));
		return result;
	}

	public static void main(String[] args) {
		System.out.println(DateUtil.age(DateUtil.parse("19910227"), new Date()));
	}

}
