package com.crm.manager.controller;

import com.crm.manager.service.UserRoleService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.ManagerConstant;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.user.role.UserAllRoleDto;
import com.crm.rely.backend.core.dto.user.role.UserNodeDisplayDto;
import com.crm.rely.backend.core.dto.user.role.UserRoleDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.model.entity.user.info.UserInfoEntity;
import com.crm.rely.backend.model.entity.user.role.UserRoleAddEntity;
import com.crm.rely.backend.model.entity.user.role.UserRoleOperateListEntity;
import com.crm.rely.backend.model.entity.user.role.UserRoleSearchEntity;
import com.crm.rely.backend.model.entity.user.role.UserRoleUpdateEntity;
import com.crm.rely.backend.core.exception.ControllerException;
import com.crm.rely.backend.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 角色
 */
@RestController
@RequestMapping("/user/role")
public class UserRoleController {

    @Autowired
    private UserRoleService roleInfoService;

    /**
     * 添加角色
     *
     * @param userRoleAddEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto addRoleInfo(@RequestBody @Valid UserRoleAddEntity userRoleAddEntity) throws Exception {

        if (ManagerConstant.ROLE_CODE_SYSTEM.equals(userRoleAddEntity.getCode())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }

        String roleCode = getRoleCode(userRoleAddEntity.getDepartmentId());
        userRoleAddEntity.setCode(roleCode);
        roleInfoService.addRoleInfo(userRoleAddEntity);

        return BaseResultDto.success();
    }

    @PostMapping("/add/detail/list")
    public BaseResultDto<List<UserNodeDisplayDto>> getDetailList() throws Exception {

        List<UserNodeDisplayDto> userNodeDisplayEntities = roleInfoService.getUserNodeDisplay(-1L);

        return BaseResultDto.success(userNodeDisplayEntities);
    }

    /**
     * 修改角色
     *
     * @return
     */
    @PostMapping("/update")
    public BaseResultDto updateRoleInfoBase(@RequestBody @Valid UserRoleUpdateEntity userRoleUpdateEntity) throws Exception {
        if (ManagerConstant.ROLE_CODE_SYSTEM.equals(userRoleUpdateEntity.getCode())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }


        String roleCode = getRoleCode(userRoleUpdateEntity.getDepartmentId());
        userRoleUpdateEntity.setCode(roleCode);
        roleInfoService.updateRoleInfo(userRoleUpdateEntity);

        return BaseResultDto.success();
    }

    /**
     * 修改角色权限
     *
     * @return
     */
    @PostMapping("/update/detail")
    public BaseResultDto updateRoleInfoDetail(@RequestBody @Valid UserRoleOperateListEntity userRoleOperateListEntity) throws Exception {

        roleInfoService.updateRoleInfo(userRoleOperateListEntity);

        return BaseResultDto.success();
    }

    /**
     * 拉取所有权限列表 用于修改权限的时候用
     *
     * @return
     */
    @PostMapping("/update/detail/list")
    public BaseResultDto<List<UserNodeDisplayDto>> getDetailList(@RequestBody @Valid SingleLongEntity singleLongEntity, UserInfoEntity userInfoEntity) throws Exception {

        if (userInfoEntity.getRoleId() != null && userInfoEntity.getRoleId() != 0) {
            if (singleLongEntity.getId() == null || singleLongEntity.getId() == 0) {
                throw new ControllerException("管理员权限无法修改");
            }
        }

        List<UserNodeDisplayDto> userNodeDisplayEntities = roleInfoService.getUserNodeDisplay(singleLongEntity.getId());

        return BaseResultDto.success(userNodeDisplayEntities);
    }

    /**
     * 删除角色
     *
     * @return
     */
    @PostMapping("/delete")
    public BaseResultDto deleteRoleInfo(@RequestBody @Valid SingleLongEntity singleLongEntity) throws Exception {

        roleInfoService.deleteRoleInfo(singleLongEntity.getId());

        return BaseResultDto.success();
    }

    /**
     * id查询角色信息
     *
     * @return
     */
    @PostMapping("/search/single")
    public BaseResultDto<UserRoleDto> searchSingle(@RequestBody @Valid SingleLongEntity singleLongEntity) throws Exception {

        UserRoleDto roleInfo = roleInfoService.getRoleInfo(singleLongEntity.getId());

        return BaseResultDto.success(roleInfo);
    }

    /**
     * 查询角色信息列表
     *
     * @return
     */
    @PostMapping("/search/page")
    public ResultWithPagerDto<UserRoleDto> searchListPage(@RequestBody @Valid UserRoleSearchEntity entity) throws Exception {
        if (entity.getEndDate() != null) {
            entity.setEndDate(DateUtil.operationDay(entity.getEndDate(), 1));
        }
        if (entity.getStartDate() != null) {
            entity.setStartDate(DateUtil.parseDate(entity.getStartDate()));
        }
        ResultWithPagerDto<UserRoleDto> result = roleInfoService.getListInfo(entity);

        return result;
    }

    /**
     * 查询角色信息列表
     *
     * @return
     */
    @PostMapping("/search/list")
    public BaseResultDto<List<UserAllRoleDto>> allList() throws Exception {

        List<UserAllRoleDto> result = roleInfoService.getListInfo();

        return BaseResultDto.success(result);
    }

    private String getRoleCode(Long departmentId) {
        int dId = departmentId == null ? -1 : departmentId.intValue();
        String roleCode;
        switch (dId) {
            case 1:
                roleCode = ManagerConstant.ROLE_CODE_SALE;
                break;
            case 2:
                roleCode = ManagerConstant.ROLE_CODE_FINANCE;
                break;
            case 3:
                roleCode = ManagerConstant.ROLE_CODE_MARKET;
                break;
            case 4:
                roleCode = ManagerConstant.ROLE_CODE_LEGAL_AFFAIRS;
                break;
            case 5:
                roleCode = ManagerConstant.ROLE_CODE_ADMIN;
                break;
            default:
                roleCode = ManagerConstant.ROLE_CODE_USER;
                break;
        }
        return roleCode;
    }

}
