package com.eitc.patient.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eitc.common.annotation.Log;
import com.eitc.common.core.controller.BaseController;
import com.eitc.common.core.domain.AjaxResult;
import com.eitc.common.enums.BusinessType;
import com.eitc.patient.domain.BusinessDictData;
import com.eitc.patient.service.IBusinessDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * BusinessDictDataController
 *
 * @author mtx
 * @date: 2024/06/17 16:22
 */
@Api(tags = "业务字典")
@RestController
@RequestMapping("/system/business-dict-data")
public class BusinessDictDataController extends BaseController {

    @Resource
    private IBusinessDictDataService businessDictDataService;

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping("/list")
    @ApiOperation("根据字典类型查询字典数据信息")
    public AjaxResult list(@ApiParam(name = "dictType", value = "字典类型", required = true) @RequestParam("dictType") String dictType) {
        List<BusinessDictData> list = businessDictDataService.list(Wrappers.<BusinessDictData>lambdaQuery()
                .eq(BusinessDictData::getDictType, dictType)
                .orderByAsc(BusinessDictData::getDictSort));

        return success(list);
    }

    @GetMapping("/dropdownList")
    @ApiOperation("根据字典类型查询字典下拉数据项")
    public AjaxResult dropdownList(@ApiParam(name = "dictType", value = "字典类型", required = true) @RequestParam("dictType") String dictType) {
        List<BusinessDictData> list = businessDictDataService.list(Wrappers.<BusinessDictData>lambdaQuery()
                .eq(BusinessDictData::getDictStatus, 1)
                .eq(BusinessDictData::getDictType, dictType)
                .orderByAsc(BusinessDictData::getDictSort));

        return success(list);
    }

    /**
     * 新增字典
     */
    @Log(title = "新增字典", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增字典")
    public AjaxResult add(@Valid @RequestBody BusinessDictData entry) {
        return toAjax(businessDictDataService.save(entry));
    }

    /**
     * 修改字典
     */
    @Log(title = "修改字典", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改字典")
    public AjaxResult edit(@Valid @RequestBody BusinessDictData entry) {
        if (StringUtils.isEmpty(entry.getId())) {
            return error("id不能为空");
        }
        return toAjax(businessDictDataService.updateById(entry));
    }

    /**
     * 删除字典
     */
    @Log(title = "删除字典", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    @ApiOperation("删除字典")
    public AjaxResult remove(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        return toAjax(businessDictDataService.removeBatchByIds(idList));
    }

}
