package com.ym.mec.web.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.entity.CooperationOrgan;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.page.CooperationOrganQueryInfo;
import com.ym.mec.biz.service.CooperationOrganService;
import com.ym.mec.common.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RequestMapping("cooperationOrgan")
@Api(tags = "合作单位(教学点)服务")
@RestController
public class CooperationOrganController extends BaseController {

    @Autowired
    private CooperationOrganService cooperationOrganService;
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "新增合作单位(教学点)")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('cooperationOrgan/add')")
    public Object add(CooperationOrgan cooperationOrgan) {
        cooperationOrganService.insert(cooperationOrgan);
        return succeed();
    }

    @ApiOperation(value = "删除合作单位(教学点)")
    @PostMapping("/del/{id}")
    @PreAuthorize("@pcs.hasPermissions('cooperationOrgan/del')")
    public Object del(@ApiParam(value = "合作单位(教学点)编号", required = true) @PathVariable("id") Integer id) {
        cooperationOrganService.delete(id);
        return succeed();
    }

    @ApiOperation(value = "修改合作单位(教学点)")
    @PutMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('cooperationOrgan/update')")
    public Object update(CooperationOrgan cooperationOrgan) {
        cooperationOrgan.setUpdateTime(new Date());
        cooperationOrganService.update(cooperationOrgan);
        return succeed();
    }

    @ApiOperation(value = "分页查询合作单位(教学点)列表")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('cooperationOrgan/queryPage')")
    public Object queryPage(CooperationOrganQueryInfo queryInfo) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed("用户信息获取失败");
		}
		if(!sysUser.getIsSuperAdmin()){
			Employee employee = employeeDao.get(sysUser.getId());
			if (StringUtils.isEmpty(queryInfo.getOrganId())) {
				queryInfo.setOrganId(employee.getOrganIdList());
			}else if(StringUtils.isEmpty(employee.getOrganIdList())){
				return failed("用户所在分部异常");
			}else {
				List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
				if(!list.containsAll(Arrays.asList(queryInfo.getOrganId().split(",")))){
					return failed("非法请求");
				}
			}
		}
        return succeed(cooperationOrganService.queryPage(queryInfo));
    }

    @ApiOperation(value = "根据机构编号获取合作单位(教学点)列表")
    @GetMapping("/queryByOrganId")
    @PreAuthorize("@pcs.hasPermissions('cooperationOrgan/queryByOrganId')")
    public Object queryByOrganId(String organId){
        return succeed(cooperationOrganService.queryByOrganId(organId));
    }
}
