package com.fs.company.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fs.common.exception.ServiceException;
import com.fs.common.utils.StringUtils;
import com.fs.company.param.CompanyUserChangeApplyAuditParam;
import com.fs.company.service.ICompanyUserChangeApplyService;
import com.fs.company.vo.CompanyUserChangeApplyVO;
import com.fs.core.security.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;

import javax.validation.Valid;

/**
 * 更换会员归属申请Controller
 */
@RestController
@RequestMapping("/company/apply")
public class CompanyUserChangeApplyController extends BaseController
{
    @Autowired
    private ICompanyUserChangeApplyService companyUserChangeApplyService;

    /**
     * 查询更换会员归属申请列表
     */
    @PreAuthorize("@ss.hasPermi('company:apply:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(required = false) Integer status)
    {
        Map<String, Object> map = new HashMap<>();
        map.put("status", status);
        map.put("companyId", SecurityUtils.getLoginUser().getCompany().getCompanyId());

        startPage();
        List<CompanyUserChangeApplyVO> list = companyUserChangeApplyService.selectApplyListByMap(map);
        return getDataTable(list);
    }

    /**
     * 获取更换会员归属申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:apply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(companyUserChangeApplyService.selectApplyDetailById(id));
    }

    /**
     * 获取更换会员归属申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:apply:audit')")
    @PostMapping(value = "/audit")
    public AjaxResult audit(@Valid @RequestBody CompanyUserChangeApplyAuditParam param)
    {
        if (param.getStatus() != 1 && param.getStatus() != 2) {
            throw new ServiceException("审核类型不正确");
        }

        if (param.getStatus() == 2 && StringUtils.isBlank(param.getReason())) {
            throw new ServiceException("拒绝理由不能为空");
        }

        companyUserChangeApplyService.audit(param.getId(), param.getStatus(), param.getReason(), SecurityUtils.getUsername());
        return AjaxResult.success();
    }

}
