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

import com.yihu.jw.base.service.dict.DictIcd10Service;
import com.yihu.jw.restmodel.base.dict.DictIcd10VO;
import com.yihu.jw.restmodel.web.*;
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
import com.yihu.jw.rm.base.BaseRequestMapping;
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.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.yihu.jw.entity.base.dict.DictIcd10DO;

/**
 * ICD10字典控制器
 *
 * @version <pre>
 * Author	Version		Date		Changes
 * litaohong 	1.0  		2018年08月31日 	Created
 *
 * </pre>
 * @since 1.
 */
@RestController
@RequestMapping(value = BaseRequestMapping.DictIcd10.PREFIX)
@Api(value = "ICD10字典管理", description = "ICD10字典管理服务接口", tags = {"wlyy基础服务 - ICD10字典管理服务接口"})
public class DictIcd10Endpoint extends EnvelopRestEndpoint {

    @Autowired
    private DictIcd10Service dictIcd10Service;

    @PostMapping("/icd1oCreateUpdate")
    @ApiOperation(value = "创建")
    public ObjEnvelop<DictIcd10VO> createOrupdate(
            @ApiParam(name = "jsonData", value = "Json数据", required = true)
            @RequestParam(value = "jsonData") String jsonData) throws Exception {
        try {
            System.out.println(jsonData);
            DictIcd10DO dictIcd10 = toEntity(jsonData, DictIcd10DO.class);
            dictIcd10Service.saveIcd10(dictIcd10);
            return success(dictIcd10, DictIcd10VO.class);
        }catch (Exception e){
            return failedObjEnvelopException(e);
        }

    }

    @PostMapping(value = BaseRequestMapping.DictIcd10.DELETE)
    @ApiOperation(value = "删除")
    public Envelop delete(
            @ApiParam(name = "ids", value = "id串，中间用,分隔", required = true)
            @RequestParam(value = "ids") String ids) {
        dictIcd10Service.delete(ids.split(","));
        return success("删除成功");
    }

    @GetMapping(value = BaseRequestMapping.DictIcd10.PAGE)
    @ApiOperation(value = "获取分页")
    public PageEnvelop<DictIcd10VO> page(
            @ApiParam(name = "fields", value = "返回的字段，为空返回全部字段")
            @RequestParam(value = "fields", required = false) String fields,
            @ApiParam(name = "filters", value = "过滤器，为空检索所有条件")
            @RequestParam(value = "filters", required = false) String filters,
            @ApiParam(name = "sorts", value = "排序，规则参见说明文档")
            @RequestParam(value = "sorts", required = false) String sorts,
            @ApiParam(name = "page", value = "分页大小", required = true, defaultValue = "1")
            @RequestParam(value = "page") int page,
            @ApiParam(name = "size", value = "页码", required = true, defaultValue = "15")
            @RequestParam(value = "size") int size) throws Exception {
        List<DictIcd10DO> dictIcd10s = dictIcd10Service.search(fields, filters, sorts, page, size);
        int count = (int) dictIcd10Service.getCount(filters);
        return success(dictIcd10s, count, page, size, DictIcd10VO.class);
    }

    @GetMapping("/findIcd10List")
    @ApiOperation(value = "获取列表")
    public MixEnvelop list(
            @ApiParam(name = "name", value = "")
            @RequestParam(value = "name", required = false) String name,
            @ApiParam(name = "status", value = "")
            @RequestParam(value = "status", required = false) String status,
            @ApiParam(name = "page", value = "")
            @RequestParam(value = "page", required = false) Integer page,
            @ApiParam(name = "pageSize", value = "")
            @RequestParam(value = "pageSize", required = false) Integer pageSize) throws Exception {
        return dictIcd10Service.findIcd10List(name, status, page,pageSize);
    }

    @GetMapping("/findIcd10One")
    @ApiOperation(value = "获取单条")
    public Envelop findIcd10One(
            @ApiParam(name = "id", value = "")
            @RequestParam(value = "id", required = false) Integer id) throws Exception {
        return success(dictIcd10Service.findOneIcd10(id));
    }

    @GetMapping("/updateIcd10Status")
    @ApiOperation(value = "修改状态")
    public Envelop updateIcd10Status(@ApiParam(name = "id", value = "id")
                                       @RequestParam(name = "id", required = true) Integer id,
                                       @ApiParam(name = "status", value = "状态1使用0作废")
                                       @RequestParam(name = "status", required = false) String status,
                                       @ApiParam(name = "del", value = "0删除")
                                       @RequestParam(name = "del", required = false) String del){

        try {
            dictIcd10Service.updateIcd10Status(id,status,del);
            return success("操作成功");
        }catch (Exception e) {
            return failedException(e);
        }
    }

}
