package jnpf.personnels.controller.web.authoritys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.vo.PageListVO;
import jnpf.model.cultivate.CultivatePage;
import jnpf.model.personnels.dto.authoritys.FtbAddNewPermissionsBatchDTO;
import jnpf.model.personnels.dto.authoritys.FtbAddNewPermissionsDTO;
import jnpf.model.personnels.dto.authoritys.FtbAddNewPermissionsUpdateDTO;
import jnpf.model.personnels.dto.authoritys.FtbPermissionInfoDTO;
import jnpf.model.personnels.vo.authoritys.FtbPermissionInfoVO;
import jnpf.model.personnels.vo.authoritys.FtbPersonnelsPermissionUserVO;
import jnpf.model.personnels.vo.authoritys.FtbPersonnelsPermissionVO;
import jnpf.model.personnels.vo.authoritys.FtbPersonnelsScopeVO;
import jnpf.model.personnels.vo.uchisuike.FtbinternalRecommendationPoolVO;
import jnpf.personnels.service.FtbPersonnelsPermissionsService;
import jnpf.util.UserProvider;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * web人事调离员工权限模块
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/web/personnels-permission-user")
public class FtbPersonnelsPermissionUserController {

    @Resource
    private FtbPersonnelsPermissionsService ftbPersonnelsPermissionsService;
    /**
     * 删除权限
     *
     * @param id 员工权限主键id（必传）
     * @return {@link ActionResult}
     */
    @DeleteMapping("/delete-permissions/{id}")
    public ActionResult deletePermissions(@PathVariable(value = "id") String id) {
        ftbPersonnelsPermissionsService.deletePermissions(id);
        return ActionResult.success();
    }

    /**
     * 权限列表
     *
     * @return {@link ActionResult}<{@link PageListVO}<{@link FtbinternalRecommendationPoolVO}>>
     */
    @GetMapping("/internal-recommendation-pool-list-query")
    public ActionResult<PageListVO<FtbPermissionInfoVO>> permissionList(CultivatePage cultivatePage,
                                                                        FtbPermissionInfoDTO ftbPermissionInfoDTO) {
        Page<FtbPermissionInfoVO> page = cultivatePage.coverCultivatePage("a.F_CreatorTime");
        ftbPermissionInfoDTO.setPermissionType(0);
        page = ftbPersonnelsPermissionsService.permissionList(page, ftbPermissionInfoDTO);
        PageListVO<FtbPermissionInfoVO> result = CultivatePage.coverPageList(page);
        return ActionResult.success(result);
    }

    /**
     * 管理按钮时查询权限详情
     *
     * @param id 员工权限主键id（必传）
     * @return {@link ActionResult}
     */
    @GetMapping("/admin-details")
    public ActionResult<FtbPersonnelsPermissionUserVO> adminDetails(String id) {
        FtbPersonnelsPermissionUserVO ftbPersonnelsPermissionUserVO = ftbPersonnelsPermissionsService.adminDetails(id, 0L);
        return ActionResult.success(ftbPersonnelsPermissionUserVO);
    }

    /**
     * 新增权限
     *
     * @return {@link ActionResult}
     */
    @PostMapping("/admin-details")
    @Transactional
    @SuppressWarnings("Duplicates")
    public ActionResult addNewPermissions(@RequestBody @Validated FtbAddNewPermissionsBatchDTO ftbAddNewPermissionsDTO) {
        List<FtbAddNewPermissionsDTO> ftbAddNewPermissionsDTOs = new ArrayList<>();
        for (FtbAddNewPermissionsBatchDTO.UserInnerInfo userInnerInfo : ftbAddNewPermissionsDTO.getUserInnerInfos()) {
            FtbAddNewPermissionsDTO temp = new FtbAddNewPermissionsDTO();
            temp.setPermissionType(0);
            temp.setUserId(userInnerInfo.getUserId());
            temp.setUserName(userInnerInfo.getUserName());
            temp.setPhone(userInnerInfo.getPhone());
            temp.setUserCustomId(userInnerInfo.getUserCustomId());
            temp.setScopePermission(ftbAddNewPermissionsDTO.getScopePermission());
            temp.setPermissionIds(ftbAddNewPermissionsDTO.getPermissionIds());
            temp.setSpecifyOrgIds(ftbAddNewPermissionsDTO.getSpecifyOrgIds());

            temp.setPostIds(userInnerInfo.getPostIds());
            temp.setOrgIds(userInnerInfo.getOrgIds());
            temp.setPostRankIds(userInnerInfo.getPostRankIds());

            temp.setOrgNames(userInnerInfo.getOrgNames());
            temp.setPostNames(userInnerInfo.getPostNames());
            temp.setPostRankNames(userInnerInfo.getPostNameIds());

            ftbAddNewPermissionsDTOs.add(temp);
        }
        for (FtbAddNewPermissionsDTO t : ftbAddNewPermissionsDTOs) {
            ftbPersonnelsPermissionsService.addNewPermissions(t);
        }
        return ActionResult.success();
    }

    /**
     * 修改权限
     *
     * @return {@link ActionResult}
     */
    @PutMapping("/admin-details-change")
    public ActionResult addNewPermissionsChange(@RequestBody @Validated FtbAddNewPermissionsUpdateDTO ftbAddNewPermissionsUpdateDTO) {
        ftbAddNewPermissionsUpdateDTO.setPermissionType(0);
        ftbPersonnelsPermissionsService.addNewPermissionsChange(ftbAddNewPermissionsUpdateDTO);
        return ActionResult.success();
    }

    /**
     * 根据父级权限查询按钮权限列表(返回权限标识)
     *
     * @param parentPermissions 父级权限标识(必传)
     * @return {@link ActionResult}<{@link List}
     */
    @GetMapping("/query-permission")
    public ActionResult<List<String>> queryPermissionList(String parentPermissions) {
        UserInfo userInfo = UserProvider.getUser();
        List<String> result = ftbPersonnelsPermissionsService.queryPermissionList(userInfo, parentPermissions, 0);
        return ActionResult.success(result);
    }

    /**
     * 新增时查询权限列表
     */
    @GetMapping("/query-the-permission-list-when-adding")
    public ActionResult<List<FtbPersonnelsPermissionVO>> queryThePermissionListWhenAdding() {
        List<FtbPersonnelsPermissionVO> ftbPersonnelsPermissionVOS = ftbPersonnelsPermissionsService.queryThePermissionListWhenAdding(0L);
        return ActionResult.success(ftbPersonnelsPermissionVOS);
    }

    /**
     * 获取当前登录人所具有的组织范围(返回组织id)
     *
     * @return {@link ActionResult }
     */
    @GetMapping("/select-organization-scope")
    public ActionResult<FtbPersonnelsScopeVO> selectOrganizationScope() {
        FtbPersonnelsScopeVO result = ftbPersonnelsPermissionsService.selectOrganizationScope();
        return ActionResult.success(result);
    }

    /**
     * 获取当前登录人所具有的员工范围(返回员工id)
     *
     * @return {@link ActionResult }
     */
    @GetMapping("/select-people-range")
    public ActionResult<FtbPersonnelsScopeVO> selectEmployeeRange() {
        FtbPersonnelsScopeVO result = ftbPersonnelsPermissionsService.selectEmployeeRange();
        return ActionResult.success(result);
    }

    /**
     * 此用户是否是员工，true为是，false为否
     */
    @GetMapping("/is-this-user-an-employee")
    public ActionResult<Boolean> isThisUserAnEmployee() {
        Boolean result = ftbPersonnelsPermissionsService.isThisUserAnEmployee();
        return ActionResult.success(result);
    }


}
