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

import com.alibaba.fastjson.JSONObject;
import com.yihu.jw.base.service.doctor.BaseDoctorRoleDictService;
import com.yihu.jw.base.service.doctor.BaseDoctorRoleInfoService;
import com.yihu.jw.base.util.ConstantUtils;
import com.yihu.jw.entity.base.doctor.BaseDoctorRoleDictDO;
import com.yihu.jw.entity.base.doctor.BaseDoctorRoleInfoDO;
import com.yihu.jw.restmodel.base.doctor.BaseDoctorRoleInfoVO;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 业务模块角色字典（给医生用的）控制器
 *
 * @version <pre>
 * Author	Version		Date		Changes
 * litaohong 	1.0  		2018年10月25日 	Created
 *
 * </pre>
 * @since 1.
 */
@RestController
@RequestMapping(value = BaseRequestMapping.BaseDoctorRoleInfo.PREFIX)
@Api(value = "业务模块角色字典（给医生用的）管理", description = "业务模块角色字典（给医生用的）管理服务接口", tags = {"wlyy基础服务 - 业务模块角色字典（给医生用的）管理服务接口"})
public class BaseDoctorRoleInfoEndpoint extends EnvelopRestEndpoint {

    @Autowired
    private BaseDoctorRoleInfoService baseDoctorRoleInfoService;
    @Autowired
    private BaseDoctorRoleDictService baseDoctorRoleDictService;


    @PostMapping(value = BaseRequestMapping.BaseDoctorRoleInfo.CREATE)
    @ApiOperation(value = "创建")
    public Envelop create(
            @ApiParam(name = "jsonData", value = "Json数据", required = true)
            @RequestParam(value = "jsonData", required = true)  String jsonData )throws Exception {
        JSONObject jsonObject = baseDoctorRoleInfoService.createRoleInfo(jsonData,getUserAgent());
        if (jsonObject.getString("response").equalsIgnoreCase(ConstantUtils.FAIL)) {
            return failed(jsonObject.getString("msg"));
        }
        return success(jsonObject.getString("msg"));
    }

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

    @PostMapping(value = BaseRequestMapping.BaseDoctorRoleInfo.UPDATE)
    @ApiOperation(value = "更新")
    public Envelop update(
            @ApiParam(name = "jsonData", value = "Json数据", required = true)
            @RequestParam(value = "jsonData", required = true)  String jsonData ) throws Exception {
        JSONObject jsonObject = baseDoctorRoleInfoService.updateRoleInfo(jsonData);
        if (jsonObject.getString("response").equalsIgnoreCase(ConstantUtils.FAIL)) {
            return failed(jsonObject.getString("msg"));
        }
        return success(jsonObject.getString("msg"));
    }

    @GetMapping(value = BaseRequestMapping.BaseDoctorRoleInfo.PAGE)
    @ApiOperation(value = "获取角色信息列表分页")
    public PageEnvelop<BaseDoctorRoleInfoVO> 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<BaseDoctorRoleInfoDO> baseDoctorRoleInfos = baseDoctorRoleInfoService.search(fields, filters, sorts, page, size);
        int count = (int) baseDoctorRoleInfoService.getCount(filters);
        return success(baseDoctorRoleInfos, count, page, size, BaseDoctorRoleInfoVO.class);
    }

    @GetMapping(value = BaseRequestMapping.BaseDoctorRoleInfo.LIST)
    @ApiOperation(value = "获取角色信息列表，无分页")
    public ListEnvelop<BaseDoctorRoleDictDO> 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<BaseDoctorRoleDictDO> baseDoctorRoleInfos = baseDoctorRoleDictService.search(fields, filters, sorts);
        return success(baseDoctorRoleInfos, BaseDoctorRoleDictDO.class);
    }

    @GetMapping(value = BaseRequestMapping.BaseDoctorRoleInfo.queryOne)
    @ApiOperation(value = "查询某一个角色")
    public Envelop getTeamById(
            @ApiParam(name = "roleId", value = "根据角色标识查询某一个角色信息")
            @RequestParam(value = "roleId", required = true) Integer roleId) throws Exception {
        JSONObject team = baseDoctorRoleInfoService.getOneRoleInfo(roleId);
        if (team.getString("response").equalsIgnoreCase(ConstantUtils.FAIL)) {
            return failed(team.getString("msg"));
        }
        return success(team.getJSONObject("msg"));
    }

    @GetMapping(value = BaseRequestMapping.BaseDoctorRoleInfo.moduleTree)
    @ApiOperation(value = "查询业务模块树形结构")
    public Envelop queryModuleTree(
            @ApiParam(name = "saasId", value = "租户id")
            @RequestParam(value = "saasId", required = true) String saasId,
            @ApiParam(name = "roleCode", value = "角色code")
            @RequestParam(value = "roleCode", required = false) String roleCode) throws Exception {
        JSONObject moduleTree = baseDoctorRoleInfoService.getModuleTreeBySaas(saasId,roleCode);
        if (moduleTree.getString("response").equalsIgnoreCase(ConstantUtils.FAIL)) {
            return failed(moduleTree.getString("msg"));
        }
        return success(moduleTree.get("msg"));
    }


    /**
     * 生效或失效某个角色
     *
     * @param id
     * @param status
     * @return
     * @throws Exception
     */
    @PostMapping(value = BaseRequestMapping.BaseDoctorRoleInfo.enableOrDis)
    @ApiOperation(value = "生效或失效某个角色")
    public Envelop enableOrDisableOrg(
            @ApiParam(name = "id", value = "角色Id")
            @RequestParam(value = "id", required = true) Integer id,
            @ApiParam(name = "status", value = "生效或失效标识")
            @RequestParam(value = "status", required = true) String status) throws Exception {
        JSONObject jsonObject = baseDoctorRoleInfoService.enableOrDisable(id, status);
        if (jsonObject.getString("response").equalsIgnoreCase(ConstantUtils.FAIL)) {
            return failed(jsonObject.getString("msg"));
        }
        return success(jsonObject.getString("response"));
    }

    @GetMapping(value = BaseRequestMapping.BaseDoctorRoleInfo.checkRoleName)
    @ApiOperation(value = "检查角色名称是否可用(message=available代表可用，message=inavailable代表不可用)")
    public Envelop checkRoleName (
            @ApiParam(name = "saasid", value = "租户id", required = true)
            @RequestParam(value = "saasid", required = false) String saasid,
            @ApiParam(name = "name", value = "角色名称", required = true)
            @RequestParam(value = "name", required = false) String name) throws Exception {
        JSONObject jsonObject = baseDoctorRoleInfoService.existNameOfSaas(saasid, name);
        if (jsonObject.getString("response").equalsIgnoreCase(ConstantUtils.FAIL)) {
            return failed(jsonObject.getString("msg"));
        }
        if(jsonObject.getBoolean("msg")){
            return success("available");
        }else{
            return success("inavailable");
        }
    }


}
