package com.app.controller.platform;

import com.app.annotation.platform.OperationLog;
import com.app.dto.PageResult;
import com.app.dto.Result;
import com.app.dto.SearchDto;
import com.app.dto.platform.SearchDictDto;
import com.app.dto.platform.SearchDictEntryDto;
import com.app.entity.platform.system.Dict;
import com.app.entity.platform.system.DictEntry;
import com.app.service.platform.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 字典管理
 *
 * @Auther: 梁展鹏
 * @Date: 2018/7/24 11:38
 */
@Api(tags = "字典管理")
@Slf4j
@RestController("PlatformDictController")
@RequestMapping("/platform/dict")
public class DictController {

    @Resource
    private DictService dictService;

    /**
     * 查询【字典】列表
     *
     * @param params 搜索dto
     * @return 响应结果
     */
    @ApiOperation(value = "查询[字典]列表")
    @PostMapping("/page")
    public ResponseEntity<Result<PageResult<Dict>>> page(@Valid @RequestBody SearchDictDto params) {
        return new ResponseEntity(new Result<>().ok(dictService.page(params)), HttpStatus.OK);
    }

    /**
     * 保存/更新[字典]
     *
     */
    @OperationLog("更新 - 字典")
    @ApiOperation(value = "保存/更新[字典]")
    @PostMapping("/save-or-update")
    public ResponseEntity<Result> saveOrUpdate(@Valid @RequestBody Dict dict) {
        if(dictService.saveOrUpdate(dict)!=null){
            return new ResponseEntity(new Result().ok(), HttpStatus.OK);
        }
        return new ResponseEntity(new Result().serverError("字典名称或已存在"), HttpStatus.OK);
    }

    /**
     * 删除【字典】
     */
    @OperationLog("替换删除状态 - 字典")
    @ApiOperation(value = "替换[字典]删除状态")
    @PostMapping("/handle-is-del")
    public ResponseEntity<Result> handleIsDel(@Valid @RequestBody SearchDto searchdto) {
        dictService.handleIsDel(searchdto.getId());
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }


    /**
     * 查询【部门实体】列表
     *
     * @param params 搜索dto
     * @return 响应结果
     */
    @ApiOperation(value = "查询[部门实体]列表")
    @PostMapping("/entry/list")
    public ResponseEntity<Result<List<DictEntry>>> entryList(@Valid @RequestBody SearchDictEntryDto params) {
        return new ResponseEntity(new Result<>().ok(dictService.entryList(params)), HttpStatus.OK);
    }

    /**
     * 保存/更新[部门实体]
     *
     */
    @OperationLog("更新 - 部门实体")
    @ApiOperation(value = "保存/更新[部门实体]")
    @PostMapping("/entry/save-or-update")
    public ResponseEntity<Result> entrySaveOrUpdate(@Valid @RequestBody DictEntry entry) {
        if(dictService.entrySaveOrUpdate(entry)!=null){
            return new ResponseEntity(new Result().ok(), HttpStatus.OK);
        }
        return new ResponseEntity(new Result().serverError("字典名称或已存在"), HttpStatus.OK);
    }

    /**
     * 删除【部门实体】
     */
    @OperationLog("替换删除状态 - 部门实体")
    @ApiOperation(value = "替换[部门实体]删除状态")
    @PostMapping("/entry/handle-is-del")
    public ResponseEntity<Result> entryHandleIsDel(@Valid @RequestBody SearchDto searchdto) {
        dictService.entryHandleIsDel(searchdto.getId());
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }

}
