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

import com.yihu.jw.base.service.dict.BaseDrugDictService;
import com.yihu.jw.base.service.dict.BaseDrugUseService;
import com.yihu.jw.entity.base.dict.BaseDrugDictDO;
import com.yihu.jw.entity.base.dict.BaseDrugUseDictDO;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.restmodel.web.ObjEnvelop;
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.*;

@RestController
@RequestMapping(value = "/baseDrugUse")
@Api(value = "药品使用方式字典", description = "药品使用方式字典", tags = {"药品使用方式字典"})
public class BaseDrugUseEndpoint extends EnvelopRestEndpoint {
    @Autowired
    private BaseDrugUseService baseDrugUseService;

    @PostMapping("/saveDrugUse")
    @ApiOperation(value = "保存")
    public Envelop saveDrugUse(@ApiParam(name = "jsonData", value = "药品json串")
                                       @RequestParam(name = "jsonData", required = true) String jsonData) throws Exception {

        try {
            BaseDrugUseDictDO baseDrugUseDictDO = objectMapper.readValue(jsonData, BaseDrugUseDictDO.class);
            baseDrugUseService.saveBaseDrugUse(baseDrugUseDictDO);
            return success("操作成功");
        }catch (Exception e) {
            return failedObjEnvelopException(e);
        }

    }

    @GetMapping("/findDrugUseList")
    @ApiOperation(value = "列表")
    public MixEnvelop findDrugDict(@ApiParam(name = "name", value = "药品名称")
                                   @RequestParam(name = "name", required = false) String name,
                                   @ApiParam(name = "status", value = "状态")
                                   @RequestParam(name = "status", required = false) String status,
                                   @ApiParam(name = "page", value = "当前页")
                                   @RequestParam(name = "page", required = false) Integer page,
                                   @ApiParam(name = "pageSize", value = "页面大小")
                                   @RequestParam(name = "pageSize", required = false) Integer pageSize) throws Exception {

        try {
            return baseDrugUseService.findDrugUseList(name,status, page, pageSize);
        }catch (Exception e) {
            return failedMixEnvelopException(e);
        }
    }

    @GetMapping("/findOneDrug")
    @ApiOperation(value = "根据id查询")
    public Envelop findOneDrug(@ApiParam(name = "id", value = "药品id")
                               @RequestParam(name = "id", required = true) String id){

        try {

            return success(baseDrugUseService.findOneUseDict(id));
        }catch (Exception e) {
            return failedException(e);
        }
    }

    @GetMapping("/updateDruguseStatus")
    @ApiOperation(value = "修改状态")
    public Envelop updateDruguseStatus(@ApiParam(name = "id", value = "id")
                               @RequestParam(name = "id", required = true) String 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 {
            baseDrugUseService.updateDrugUseStatus(id,status,del);
            return success("操作成功");
        }catch (Exception e) {
            return failedException(e);
        }
    }
}
