package com.mmc.iuav.user.controller;

import com.mmc.iuav.group.Insert;
import com.mmc.iuav.response.ResultBody;
import com.mmc.iuav.user.model.qo.CompanyInfoQO;
import com.mmc.iuav.user.model.vo.CompanyInfoVO;
import com.mmc.iuav.user.model.vo.UserAccountVO;
import com.mmc.iuav.user.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: zj
 * @Date: 2023/7/3 15:35
 */
@Api(tags = "单位管理")
@RequestMapping("/company/")
@RestController
public class CompanyController extends BaseController {
    @Autowired
    private CompanyService companyService;

    @ApiOperation(value = "新增")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @PostMapping("add")
    public ResultBody add(@Validated(Insert.class) @RequestBody CompanyInfoVO companyInfo) {
        return companyService.addCompanyInfo(companyInfo);
    }

    @ApiOperation(value = "移除")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @GetMapping("remove")
    public ResultBody remove(@RequestParam Integer id, @RequestHeader String token) {
        return companyService.removeCompanyInfo(id, token);
    }

    @ApiOperation(value = "修改")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @PostMapping("update")
    public ResultBody update(@RequestBody CompanyInfoVO companyInfo) {
        return companyService.updateCompanyInfo(companyInfo);
    }

    @ApiOperation(value = "单位查询")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = CompanyInfoVO.class)})
    @GetMapping("getCompanyInfoById")
    public ResultBody<CompanyInfoVO> getCompanyInfoById(@RequestParam Integer id) {
        return companyService.getCompanyInfoById(id);
    }

    @ApiOperation(value = "pc-后台用户id单位查询")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = CompanyInfoVO.class)})
    @GetMapping("getCompanyInfoByBUId")
    public ResultBody<CompanyInfoVO> getCompanyInfoByBUId(@RequestParam Integer backUserAccountId) {
        return companyService.getCompanyInfoByBUId(backUserAccountId);
    }

    @ApiOperation(value = "单位列表")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = CompanyInfoVO.class)})
    @PostMapping("listCompanyPage")
    public ResultBody<CompanyInfoVO> listCompanyPage(@RequestBody CompanyInfoQO companyInfoQO, HttpServletRequest request) {
        return companyService.listCompanyPage(companyInfoQO, this.getUserLoginInfoFromRedis(request));
    }

    @ApiIgnore
    @ApiOperation(value = "单位列表-后台使用")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = CompanyInfoVO.class)})
    @PostMapping("listCompanyPageBack")
    public List<CompanyInfoVO> listCompanyPageBack(@RequestBody CompanyInfoQO companyInfoQO) {
        return companyService.listCompanyPageBack(companyInfoQO);
    }

    @ApiOperation(value = "成员-绑定")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @GetMapping("bindingCompanyMember")
    public ResultBody bindingCompanyMember(HttpServletRequest request,
                                           @RequestParam Integer userAccountId,
                                           @RequestParam Integer companyInfoId) {
        return companyService.bindingCompanyMember(this.getUserLoginInfoFromRedis(request), userAccountId, companyInfoId);
    }

    @ApiOperation(value = "成员-解绑")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @GetMapping("unbindCompanyMember")
    public ResultBody unbindCompanyMember(HttpServletRequest request,
                                          @RequestParam Integer userAccountId,
                                          @RequestParam Integer companyInfoId) {
        return companyService.unbindCompanyMember(this.getUserLoginInfoFromRedis(request), userAccountId, companyInfoId);
    }

    @ApiOperation(value = "成员-转让管理员")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @GetMapping("transferLeader")
    public ResultBody transferLeader(HttpServletRequest request,
                                     @RequestParam Integer fromUserAccountId,
                                     @RequestParam Integer toUserAccountId,
                                     @RequestParam Integer companyInfoId) {
        return companyService.transferLeader(this.getUserLoginInfoFromRedis(request), fromUserAccountId, toUserAccountId, companyInfoId);
    }

    @ApiOperation(value = "成员-列表")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @GetMapping("listCompanyMembers")
    public ResultBody<UserAccountVO> listCompanyMembers(HttpServletRequest request,
                                                        @RequestParam Integer companyInfoId,
                                                        @RequestParam Integer pageNo,
                                                        @RequestParam Integer pageSize) {
        return companyService.listCompanyMembers(companyInfoId, this.getUserLoginInfoFromRedis(request), pageNo, pageSize);
    }

    @ApiIgnore
    @ApiOperation(value = "批量获取单位信息")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = CompanyInfoVO.class)})
    @PostMapping("listCompanyInfoByIds")
    public List<CompanyInfoVO> listCompanyInfoByIds(@RequestBody List<Integer> ids) {
        return companyService.listCompanyInfoByIds(ids);
    }

    @ApiOperation(value = "批量获取单位信息-后台用户id批量获取")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = CompanyInfoVO.class)})
    @PostMapping("listCompanyInfoByBUIds")
    public List<CompanyInfoVO> listCompanyInfoByBUIds(@RequestBody List<Integer> backUserAccountIds) {
        return companyService.listCompanyInfoByBUIds(backUserAccountIds);
    }
}
