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

import com.yihu.jw.base.dao.servicePackage.ServicePackageDao;
import com.yihu.jw.base.dao.servicePackage.ServicePackageItemDao;
import com.yihu.jw.base.service.servicePackage.ServicePackageService;
import com.yihu.jw.entity.base.servicePackage.ServicePackageDO;
import com.yihu.jw.entity.base.servicePackage.ServicePackageItemDO;
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 com.yihu.jw.rm.base.BaseRequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
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.stream.Collectors;

/**
 * @author yeshijie on 2018/8/29.
 */
@RestController
@RequestMapping(value = BaseRequestMapping.ServicePackage.PREFIX, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(value = "服务包管理", description = "服务包管理服务接口", tags = {"wlyy基础服务 - 服务包管理服务接口"})
public class ServicePackageEndpoint extends EnvelopRestEndpoint {

    @Autowired
    private ServicePackageService servicePackageService;
    @Autowired
    private ServicePackageDao servicePackageDao;
    @Autowired
    private ServicePackageItemDao servicePackageItemDao;
    @Autowired
    private WlyyHospitalSysDictDao wlyyHospitalSysDictDao;

    @PostMapping(value = BaseRequestMapping.ServicePackage.CREATE)
    @ApiOperation(value = "创建")
    public Envelop create (
            @ApiParam(name = "jsonData", value = "Json数据", required = true)
            @RequestParam String jsonData) throws Exception {
        ServicePackageDO servicePackageDO = toEntity(jsonData, ServicePackageDO.class);
        servicePackageService.create(servicePackageDO);
        return success("操作成功");
    }

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


    @GetMapping(value = BaseRequestMapping.ServicePackage.PAGE)
    @ApiOperation(value = "获取分页")
    public PageEnvelop<ServicePackageDO> 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<ServicePackageDO> servicePackageDOS = servicePackageService.search(fields, filters, sorts, page, size);
        List<WlyyHospitalSysDictDO> wlyyHospitalSysDictDOS = wlyyHospitalSysDictDao.findByDictName("serviceType");
        List<WlyyHospitalSysDictDO> wlyyHospitalSysDictDOS1 = wlyyHospitalSysDictDao.findByDictName("isEffect");
        for (ServicePackageDO servicePackageDO:servicePackageDOS){
            List<ServicePackageItemDO> itemDOList = servicePackageItemDao.findByServicePackageId(servicePackageDO.getId());
            List<String> orgNames = itemDOList.stream().map(p -> p.getOrgName()).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
//            List<String> teamNames = itemDOList.stream().map(p -> p.getTeamName()).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
            List<String> orgCodes = itemDOList.stream().map(p -> p.getOrgCode()).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
 //           List<String> teamCodes = itemDOList.stream().map(p -> p.getTeamCode()).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());

            servicePackageDO.setOrgNames(StringUtils.join(orgNames,","));
//            servicePackageDO.setTeamNames(StringUtils.join(teamNames,","));
            servicePackageDO.setOrgCodes(StringUtils.join(orgCodes,","));

            for (WlyyHospitalSysDictDO wlyyHospitalSysDictDO:wlyyHospitalSysDictDOS){
                if (servicePackageDO.getType().equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                    servicePackageDO.setTypeName(wlyyHospitalSysDictDO.getDictValue());
                }
            }

            for (WlyyHospitalSysDictDO wlyyHospitalSysDictDO:wlyyHospitalSysDictDOS1){
                if (servicePackageDO.getDel().equalsIgnoreCase(wlyyHospitalSysDictDO.getDictCode())){
                    servicePackageDO.setDelName(wlyyHospitalSysDictDO.getDictValue());
                }
            }
        }
        int count = (int)servicePackageService.getCount(filters);
        return success(servicePackageDOS, count, page, size);
    }

    @GetMapping(value = "findById")
    @ApiOperation(value = "按id查找")
    public ObjEnvelop<ServicePackageDO> findById (
            @ApiParam(name = "id", value = "id")
            @RequestParam(value = "id", required = true) String id) throws Exception {
        ServicePackageDO servicePackageDO = servicePackageDao.findOne(id);
        List<ServicePackageItemDO> itemDOList = servicePackageItemDao.findByServicePackageId(servicePackageDO.getId());
        List<String> orgNames = itemDOList.stream().map(p -> p.getOrgName()).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
//        List<String> teamNames = itemDOList.stream().map(p -> p.getTeamName()).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
        servicePackageDO.setOrgNames(StringUtils.join(orgNames,","));
//        servicePackageDO.setTeamNames(StringUtils.join(teamNames,","));
        servicePackageDO.setPackageItemDOList(itemDOList);
        return success(servicePackageDO);
    }

    @GetMapping(value = BaseRequestMapping.ServicePackage.LIST)
    @ApiOperation(value = "获取列表")
    public ListEnvelop<ServicePackageDO> 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<ServicePackageDO> servicePackageDOS = servicePackageService.search(fields, filters, sorts);
        return success(servicePackageDOS);
    }
}
