package com.xhpc.general.controller;

import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.common.log.annotation.Log;
import com.xhpc.common.log.enums.BusinessType;
import com.xhpc.general.domain.XhpcDictionary;
import com.xhpc.general.dto.XhpcDictionaryChild;
import com.xhpc.general.service.IXhpcDictBizService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 业务字典Controller
 *
 * @author yuyang
 * @date 2021-07-19
 */
@RestController
@RequestMapping("/dictBiz")
public class XhpcDictBizController extends BaseController {

    @Autowired
    private IXhpcDictBizService xhpcDictBizService;
    /**
     * 根据code获取数据(通用接口)
     */
    //@PreAuthorize(hasPermi = "system:station:remove")
    //@Log(title = "电站", businessType = BusinessType.DELETE)
    @GetMapping(value = "/getCode")
    public AjaxResult getCode(@Param("code")String code)
    {
        return AjaxResult.success(xhpcDictBizService.getCode(code));
    }

    /**Adding a parent item to the table(xhpc_dict_biz)
     *
     * @param name the dictionary's name
     * @param code the dictionary's code
     * @param sort the dictionary's sortValue
     * @return
     */
    @Log(title = "字典-添加", businessType = BusinessType.INSERT)
    @PostMapping("/addMainItem")
    public AjaxResult addMainDictionaryItem(@RequestBody XhpcDictionary xhpcDictionary){

        return xhpcDictBizService.addADictionaryItem(xhpcDictionary.getDictValue(),xhpcDictionary.getCode(),xhpcDictionary.getSort());
    }


    /**
     * Getting a dictionary data.
     *
     * @param id The id is  map to dictionary table is dict_biz_id.
     * @return
     */
    @GetMapping("/getDictionaryItem")
    public AjaxResult getADictionaryItem(@RequestParam("id") Long id){

        return AjaxResult.success(xhpcDictBizService.selectDictionaryItem(id));
    }





    /**
     * Adding a childItem to the table(xhpc_dict_biz)
     * @param parentId
     * @param code
     * @param dictKey
     * @param dictValue
     * @return
     */
    @PostMapping("/addDetailItem")
    public AjaxResult addDetailDictionaryItem(@RequestBody XhpcDictionary xhpcDictionary){

        return xhpcDictBizService.addAChildDictionaryItem(xhpcDictionary.getParentId(), xhpcDictionary.getCode(), xhpcDictionary.getDictKey(), xhpcDictionary.getDictValue(), xhpcDictionary.getSort());
    }


    /**
     * Deleting a dictionary item by id(dict_biz_id)
     * @param id
     * @return
     */
    @PostMapping("/deleteChildDictionaryItem")
    public AjaxResult deleteChildDictionaryItem(@RequestBody XhpcDictionary xhpcDictionary){

        return xhpcDictBizService.deleteChildDictionaryItem(xhpcDictionary.getDictBizId());
    }

    @PostMapping("/deleteFather")
    public AjaxResult deleteParentItem(@RequestBody XhpcDictionary xhpcDictionary){

        return xhpcDictBizService.deleteParentItem(xhpcDictionary.getDictBizId());
    }



    /**
     * Getting a list of the parent dict items by code and name.
     * @param code
     * @param name
     * @return
     */
    @GetMapping("/getParentItems")
    public TableDataInfo getSearchParentResult(String code, String name ){

        startPage();
        List<XhpcDictionary> list = xhpcDictBizService.getMainItems(code,name);
        return getDataTable(list);
    }



    /**
     * Getting a list of the child dict items by code,code,parentId.
     * @param parentId
     * @param code
     * @param name
     * @return
     */
    @GetMapping("/getChildItems")
    public TableDataInfo getSearchChildResult(@RequestParam Long parentId, String code, String name){

        startPage();
        List<XhpcDictionary> list=xhpcDictBizService.getDetailsItems(parentId, code, name);
        return getDataTable(list);
    }


    /**
     * Updating the main item by code,dictValue,sort
     * @param dictBizId
     * @param code
     * @param keyValue the item's name
     * @param sort
     * @return
     */
    @PostMapping("/updateFatherItem")
    public AjaxResult updateFatherItem(@RequestBody XhpcDictionary xhpcDictionary){

        return xhpcDictBizService.updateMainItem(xhpcDictionary.getDictBizId(), xhpcDictionary.getCode(), xhpcDictionary.getDictValue(), xhpcDictionary.getSort());
    }


    /**
     * Updating the child item.
     * @param parentId
     * @param id
     * @param value
     * @param key
     * @param sort
     * @return
     */
    @PostMapping("/updateChildItem")
    public AjaxResult updateChildItem(@RequestBody XhpcDictionary xhpcDictionary){

        return xhpcDictBizService.updateChildItem(xhpcDictionary.getParentId(), xhpcDictionary.getDictBizId(), xhpcDictionary.getDictValue(), xhpcDictionary.getDictKey(), xhpcDictionary.getSort());
    }


    /**
     * Deleting a bundle of child item.
     * @param ids
     * @return
     */
    @PostMapping("/deleteChildItems")
    public AjaxResult deletChildItems(@RequestBody XhpcDictionaryChild xhpcDictionaryChild){

        return xhpcDictBizService.deleteChildItems(xhpcDictionaryChild.getIds());
    }
    @PostMapping("/deleteParentItems")
    public AjaxResult deleteParentItems(@RequestBody XhpcDictionaryChild xhpcDictionaryChild){

        return  xhpcDictBizService.deleteParentItems(xhpcDictionaryChild.getIds());
    }

}
