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.Employee;
import com.ym.mec.biz.dal.entity.MemberRankOrganizationFeeMapper;
import com.ym.mec.biz.dal.page.MemberRankFeeQueryInfo;
import com.ym.mec.biz.service.MemberRankOrganizationFeeMapperService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.util.collection.MapUtil;
import com.yonge.log.model.AuditLogAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("memberRankOrganizationFeeMapper")
@Api(tags = "会员收费设置服务")
@RestController
public class MemberRankOrganizationFeeMapperController extends BaseController {

    @Autowired
    private MemberRankOrganizationFeeMapperService memberRankOrganizationFeeMapperService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeDao employeeDao;

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('memberRankOrganizationFeeMapper/add')")
    @AuditLogAnnotation(operateName = "新增系统功能类型")
    public Object add(@RequestBody MemberRankOrganizationFeeMapper memberRankOrganizationFeeMapper) {
        memberRankOrganizationFeeMapperService.add(memberRankOrganizationFeeMapper);
        return succeed();
    }

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('memberRankOrganizationFeeMapper/update')")
    public Object update(@RequestBody MemberRankOrganizationFeeMapper memberRankOrganizationFeeMapper) {
        memberRankOrganizationFeeMapperService.updateFee(memberRankOrganizationFeeMapper);
        return succeed();
    }

    @ApiOperation(value = "查询")
    @PostMapping("/get")
    @PreAuthorize("@pcs.hasPermissions('memberRankOrganizationFeeMapper/get')")
    public Object get(Integer id) {
        return succeed(memberRankOrganizationFeeMapperService.get(id));
    }

    @ApiOperation(value = "获取所有的收费标准")
    @GetMapping("/findAll")
    @PreAuthorize("@pcs.hasPermissions('memberRankOrganizationFeeMapper/findAll')")
    public Object getMemberDefaultFee(MemberRankFeeQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            throw new BizException("用户信息获取失败");
        }
        Employee employee = employeeDao.get(sysUser.getId());
        if (StringUtils.isEmpty(queryInfo.getOrganId())) {
            queryInfo.setOrganId(employee.getOrganIdList());
        }else if(StringUtils.isEmpty(employee.getOrganIdList())){
            throw new BizException("用户所在分部异常");
        }else {
            List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
            if(!list.containsAll(Arrays.asList(queryInfo.getOrganId().split(",")))){
                throw new BizException("非法请求");
            }
        }
        Map<String, Object> params = new HashMap<String, Object>();
        MapUtil.populateMap(params, queryInfo);
        return succeed(memberRankOrganizationFeeMapperService.findAll(params));
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('memberRankOrganizationFeeMapper/queryPage')")
    public Object queryPage(MemberRankFeeQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            throw new BizException("用户信息获取失败");
        }
        Employee employee = employeeDao.get(sysUser.getId());
        if (StringUtils.isEmpty(queryInfo.getOrganId())) {
            queryInfo.setOrganId(employee.getOrganIdList());
        }else if(StringUtils.isEmpty(employee.getOrganIdList())){
            throw new BizException("用户所在分部异常");
        }else {
            List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
            if(!list.containsAll(Arrays.asList(queryInfo.getOrganId().split(",")))){
                throw new BizException("非法请求");
            }
        }
        return succeed(memberRankOrganizationFeeMapperService.queryPage(queryInfo));
    }
}
