package com.xmomen.module.system.controller;

import com.xmomen.framework.mybatis.page.Page;
import com.xmomen.framework.web.exceptions.ArgumentValidException;
import com.xmomen.module.logger.Log;
import com.xmomen.module.system.entity.SysDictionary;
import com.xmomen.module.system.entity.SysDictionaryParameter;
import com.xmomen.module.system.model.CreateDictionary;
import com.xmomen.module.system.model.UpdateDictionary;
import com.xmomen.module.system.service.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by Jeng on 2016/3/30.
 */
@RestController
public class DictionaryController {

    @Autowired
    DictionaryService dictionaryService;

    /**
     *  数据字典列表
     * @param group_id
     * @param limit
     * @param offset
     * @return
     */
    @RequestMapping(value = "/dictionary/{group_id}/child", method = RequestMethod.GET)
    // 查询数据字典
    @Log(actionName = "查询数据字典")
    public Page<SysDictionary> getList(
            // 分组ID
            @PathVariable(value = "group_id") Integer group_id,
            // 每页显示数量
            @RequestParam(value = "limit") Integer limit,
            // 起始位置
            @RequestParam(value = "offset") Integer offset){
        // 调用dictionaryService的getDictionaryList方法，传入分组ID、每页显示数量和起始位置，返回查询结果
        return dictionaryService.getDictionaryList(group_id, limit, offset);
    }

    /**
     *  数据字典列表
     * @param id
    // 根据group_id和id查询数据字典
     */
    // 记录日志，操作名称为查询数据字典
    @RequestMapping(value = "/dictionary/{group_id}/child/{id}", method = RequestMethod.GET)
    @Log(actionName = "查询数据字典")
    public SysDictionaryParameter get(@PathVariable(value = "group_id") Integer group_id,
        // 调用dictionaryService的getDictionary方法，根据id查询数据字典
                             @PathVariable(value = "id") Integer id){
        return dictionaryService.getDictionary(id);
    }

    /**
     * 新增数据字典
     * @param group_id
     * @param createDictionary
     * @param bindingResult
     * @return
     * @throws ArgumentValidException
     */
    @RequestMapping(value = "/dictionary/{group_id}/child", method = RequestMethod.POST)
    // 添加日志注解，记录新增数据字典的操作
    @Log(actionName = "新增数据字典")
    public SysDictionaryParameter create(@PathVariable(value = "group_id") Integer group_id,
            @RequestBody @Valid CreateDictionary createDictionary, BindingResult bindingResult) throws ArgumentValidException {
        // 如果参数校验有错误，抛出异常
        if(bindingResult != null && bindingResult.hasErrors()){
            throw new ArgumentValidException(bindingResult);
        }
        // 创建SysDictionaryParameter对象
        SysDictionaryParameter sysDictionaryParameter = new SysDictionaryParameter();
        // 设置SysDictionaryParameter对象的属性
        sysDictionaryParameter.setSysDictionaryId(createDictionary.getSysDictionaryId());
        sysDictionaryParameter.setRealValue(createDictionary.getRealValue());
        sysDictionaryParameter.setShowValue(createDictionary.getShowValue());
        sysDictionaryParameter.setSortValue(createDictionary.getSortValue());
        sysDictionaryParameter.setAvailable(createDictionary.getAvailable());
        // 调用dictionaryService的createDictionary方法，创建数据字典
        return dictionaryService.createDictionary(sysDictionaryParameter);
    }

    /**
     * 更新数据字典
     * @param group_id
     * @param id
     * @param updateDictionary
     * @param bindingResult
     * @throws ArgumentValidException
    // 根据group_id和id更新数据字典
     */
    // 记录操作日志
    @RequestMapping(value = "/dictionary/{group_id}/child/{id}", method = RequestMethod.PUT)
    @Log(actionName = "更新数据字典")
    public void update(@PathVariable(value = "group_id") Integer group_id,
                       @PathVariable(value = "id") Integer id,
        // 如果参数校验有错误，抛出异常
                           @RequestBody @Valid UpdateDictionary updateDictionary, BindingResult bindingResult) throws ArgumentValidException {
        if(bindingResult != null && bindingResult.hasErrors()){
            throw new ArgumentValidException(bindingResult);
        // 创建SysDictionaryParameter对象
        }
        // 设置id
        SysDictionaryParameter sysDictionaryParameter = new SysDictionaryParameter();
        // 设置group_id
        sysDictionaryParameter.setId(id);
        // 设置realValue
        sysDictionaryParameter.setSysDictionaryId(group_id);
        // 设置showValue
        sysDictionaryParameter.setRealValue(updateDictionary.getRealValue());
        // 设置sortValue
        sysDictionaryParameter.setShowValue(updateDictionary.getShowValue());
        // 设置available
        sysDictionaryParameter.setSortValue(updateDictionary.getSortValue());
        // 调用dictionaryService的updateDictionary方法更新数据字典
        sysDictionaryParameter.setAvailable(updateDictionary.getAvailable());
        dictionaryService.updateDictionary(sysDictionaryParameter);
    }

    /**
     * 删除数据字典
    // 根据group_id和id删除数据字典
    // 记录删除数据字典的操作
        // 调用dictionaryService的deleteDictionary方法删除数据字典
     * @param group_id
     * @param id
     */
    // 根据group_id和id删除数据字典
    @RequestMapping(value = "/dictionary/{group_id}/child/{id}", method = RequestMethod.DELETE)
    // 记录删除数据字典的操作
    @Log(actionName = "删除数据字典")
    public void delete(@PathVariable(value = "group_id") Integer group_id,
                       @PathVariable(value = "id") Integer id){
        // 调用dictionaryService的deleteDictionary方法删除数据字典
        dictionaryService.deleteDictionary(id);
    }

}
