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


import com.yihu.jw.base.service.dict.BaseDrugDictService;
import com.yihu.jw.entity.base.dict.BaseDrugDictDO;
import com.yihu.jw.entity.hospital.prescription.BaseNationalDrugDictDO;
import com.yihu.jw.hospital.prescription.service.NationalDrugDictService;
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 com.yihu.jw.rm.hospital.BaseHospitalRequestMapping;
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 = BaseHospitalRequestMapping.Prescription.PREFIX)
@Api(value = "药品字典维护", description = "药品字典维护", tags = {"药品字典维护"})
public class DictDrugEndpoint extends EnvelopRestEndpoint {
    @Autowired
    private BaseDrugDictService nationalDrugDictService;

    @PostMapping("/saveNationalDrug")
    @ApiOperation(value = "保存药品字典接口")
    public ObjEnvelop saveNationalDrug(@ApiParam(name = "jsonData", value = "药品json串")
                                       @RequestParam(name = "jsonData", required = true) String jsonData) throws Exception {

        try {
            BaseDrugDictDO nationalDrugDictDO = objectMapper.readValue(jsonData, BaseDrugDictDO.class);
            return success(nationalDrugDictService.saveNationalDrug(nationalDrugDictDO));
        }catch (Exception e) {
            return failedObjEnvelopException(e);
        }

    }

    @GetMapping("/findDrugDictList")
    @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 nationalDrugDictService.findDrugDict(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(nationalDrugDictService.findOneDrug(id));
        }catch (Exception e) {
            return failedException(e);
        }
    }

    @GetMapping("/updateDrugStatus")
    @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 {
            nationalDrugDictService.updateDrugStatus(id,status,del);
            return success("操作成功");
        }catch (Exception e) {
            return failedException(e);
        }
    }

}
