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

import com.yihu.jw.base.service.dict.DictDietService;
import com.yihu.jw.entity.care.common.DictDietDO;
import com.yihu.jw.entity.hospital.consult.WlyyHospitalSysDictDO;
import com.yihu.jw.hospital.dict.WlyyHospitalSysDictDao;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.jw.restmodel.web.ListEnvelop;
import com.yihu.jw.restmodel.web.ObjEnvelop;
import com.yihu.jw.restmodel.web.PageEnvelop;
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.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yeshijie
 * @Date: 2021/5/8
 * @Description:
 */
@RestController
@RequestMapping(value = "dictDiet", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(value = "饮食字典管理", description = "饮食字典管理服务接口", tags = {"基础服务 - 饮食字典管理服务接口"})
public class DictDietEndpoint extends EnvelopRestEndpoint {

    @Autowired
    private DictDietService dictDietService;
    @Autowired
    private WlyyHospitalSysDictDao wlyyHospitalSysDictDao;

    @PostMapping(value = "create")
    @ApiOperation(value = "创建")
    public ObjEnvelop<DictDietDO> create (
            @ApiParam(name = "jsonData", value = "Json数据", required = true)
            @RequestParam String jsonData) throws Exception {
        DictDietDO dictDiet = toEntity(jsonData, DictDietDO.class);
        dictDiet = dictDietService.save(dictDiet);
        return success(dictDiet);
    }

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

    @PostMapping(value = "update")
    @ApiOperation(value = "更新")
    public ObjEnvelop<DictDietDO> update (
            @ApiParam(name = "jsonData", value = "Json数据", required = true)
            @RequestParam String jsonData) throws Exception {
        DictDietDO dictDiet = toEntity(jsonData, DictDietDO.class);
        if (null == dictDiet.getId()) {
            return failed("ID不能为空", ObjEnvelop.class);
        }
        dictDiet = dictDietService.save(dictDiet);
        return success(dictDiet);
    }

    @GetMapping(value = "page")
    @ApiOperation(value = "获取分页")
    public PageEnvelop<DictDietDO> 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<DictDietDO> dictDiets = dictDietService.search(fields, filters, sorts, page, size);
        List<WlyyHospitalSysDictDO> foods =  wlyyHospitalSysDictDao.findByDictNameNotNull("foodClassification");
        Map<String,String> foodDict = foods.stream().collect(Collectors.toMap(WlyyHospitalSysDictDO::getDictCode,WlyyHospitalSysDictDO::getDictValue));
        dictDiets.stream().forEach(one->{
            one.setTypeName(foodDict.get(one.getType()));
        });
        int count = (int)dictDietService.getCount(filters);
        return success(dictDiets, count, page, size, DictDietDO.class);
    }

    @GetMapping(value = "list")
    @ApiOperation(value = "获取列表")
    public ListEnvelop<DictDietDO> list (
            @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) throws Exception {
        List<DictDietDO> dictDiets = dictDietService.search(fields, filters, sorts);
        return success(dictDiets, DictDietDO.class);
    }

    
}
