package com.yihu.jw.base.endpoint.dict;

import com.yihu.jw.base.dao.dict.BusinessSysDictDao;
import com.yihu.jw.base.service.dict.BusinessSysDictService;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yeshijie
 * @Date: 2021/5/10
 * @Description:
 */
@RestController
@RequestMapping(value = "businessSysDict")
@Api(value = "托育业务字典管理", description = "托育业务字典管理", tags = {"care基础服务 - 托育业务字典管理"})
public class BusinessSysDictEndpoint extends EnvelopRestEndpoint {

    @Autowired
    private BusinessSysDictService businessSysDictService;
    @Autowired
    private BusinessSysDictDao businessSysDictDao;

    @ApiOperation("查询字典")
    @GetMapping(value= "findDict")
    public MixEnvelop findDict(@ApiParam(name = "name", value = "name", required = false)
                               @RequestParam(value = "name", required = false)String name,
                               @ApiParam(name = "modelName", value = "modelName", required = false)
                               @RequestParam(value = "modelName", required = false)String modelName,
                               @ApiParam(name = "code", value = "code", required = false)
                               @RequestParam(value = "code", required = false)String code,
                               @ApiParam(name = "value", value = "value", required = false)
                               @RequestParam(value = "value", required = false)String value,
                               @ApiParam(name = "page", value = "page", required = false)
                               @RequestParam(value = "page", required = false) Integer page,
                               @ApiParam(name = "pageSize", value = "pageSize", required = false)
                               @RequestParam(value = "pageSize", required = false)Integer pageSize){

        return businessSysDictService.findDictsByNameCode(modelName,name,code,value,page,pageSize);
    }

    @ApiOperation("新增/修改字典")
    @GetMapping(value= "createOrUpdateDict")
    public Envelop createOrUpdateDict(@ApiParam(name = "json", value = "id", required = true)
                                      @RequestParam(value = "json", required = true)String json) throws Exception{

        return success(businessSysDictService.updateOrCreateDict(json));
    }

    @ApiOperation("删除字典")
    @GetMapping(value= "delDict")
    public Envelop delDict(@ApiParam(name = "id", value = "id", required = true)
                           @RequestParam(value = "id", required = true)String id){

        return success(businessSysDictService.deleteDictById(id));
    }

    @ApiOperation("查字典模块")
    @GetMapping(value= "findModelDict")
    public MixEnvelop findModelDict(@ApiParam(name = "modelName", value = "modelName", required = false)
                                    @RequestParam(value = "modelName", required = false)String modelName,
                                    @ApiParam(name = "dictName", value = "dictName", required = false)
                                    @RequestParam(value = "dictName", required = false)String dictName,
                                    @ApiParam(name = "page", value = "page", required = false)
                                    @RequestParam(value = "page", required = false) Integer page,
                                    @ApiParam(name = "pageSize", value = "pageSize", required = false)
                                    @RequestParam(value = "pageSize", required = false)Integer pageSize) throws Exception{

        return businessSysDictService.findModel(modelName,dictName,page,pageSize);
    }

    @ApiOperation("批量修改字典")
    @GetMapping(value= "updateDictGroup")
    public Envelop updateDictGroup(@ApiParam(name = "json", value = "", required = true)
                                   @RequestParam(value = "json", required = true)String json) {

        try {
            return businessSysDictService.updateByModelName(json);
        } catch (Exception e) {
            e.printStackTrace();
            return failed(e.getMessage());
        }
    }

    @ApiOperation("批量删除字典")
    @GetMapping(value= "delDictGroup")
    public Envelop delDictGroup(@ApiParam(name = "preDictName", value = "", required = true)
                                @RequestParam(value = "preDictName", required = true)String preDictName) throws Exception{

        return businessSysDictService.deleteByModelName(preDictName);
    }

    @ApiOperation("查单条")
    @GetMapping(value= "findOneDict")
    public Envelop findOneDict(@ApiParam(name = "id", value = "id", required = true)
                               @RequestParam(value = "id", required = true)String id) throws Exception{
        return success(businessSysDictDao.findOne(id));
    }
}
