package com.yc.ship.module.resource.controller.admin.drivergroup;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.framework.tenant.core.context.TenantContextHolder;
import com.yc.ship.module.resource.controller.admin.drivergroup.vo.DriverGroupPageReqVO;
import com.yc.ship.module.resource.controller.admin.drivergroup.vo.DriverGroupRespVO;
import com.yc.ship.module.resource.controller.admin.drivergroup.vo.DriverGroupSaveReqVO;
import com.yc.ship.module.resource.controller.admin.drivergroup.vo.DriverGroupSimpleRespVO;
import com.yc.ship.module.resource.controller.admin.vo.SimpleListReqVO;
import com.yc.ship.module.resource.dal.dataobject.drivergroup.DriverGroupDO;
import com.yc.ship.module.resource.enums.CommonStatusEnum;
import com.yc.ship.module.resource.service.drivergroup.DriverGroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 驾驶员分组")
@RestController
@RequestMapping("/resource/driver-group")
@Validated
public class DriverGroupController {

    @Resource
    private DriverGroupService driverGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建驾驶员分组")
    @PreAuthorize("@ss.hasPermission('dispatch:driver-group:create')")
    public CommonResult<String> createDriverGroup(@Valid @RequestBody DriverGroupSaveReqVO createReqVO) {
        return success(driverGroupService.createDriverGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新驾驶员分组")
    @PreAuthorize("@ss.hasPermission('dispatch:driver-group:update')")
    public CommonResult<Boolean> updateDriverGroup(@Valid @RequestBody DriverGroupSaveReqVO updateReqVO) {
        driverGroupService.updateDriverGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除驾驶员分组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dispatch:driver-group:delete')")
    public CommonResult<Boolean> deleteDriverGroup(@RequestParam("id") String id) {
        driverGroupService.deleteDriverGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得驾驶员分组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dispatch:driver-group:query')")
    public CommonResult<DriverGroupRespVO> getDriverGroup(@RequestParam("id") String id) {
        DriverGroupDO driverGroup = driverGroupService.getDriverGroup(id);
        return success(BeanUtils.toBean(driverGroup, DriverGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得驾驶员分组分页")
    @PreAuthorize("@ss.hasPermission('dispatch:driver-group:query')")
    public CommonResult<PageResult<DriverGroupRespVO>> getDriverGroupPage(@Valid DriverGroupPageReqVO pageReqVO) {
        PageResult<DriverGroupDO> pageResult = driverGroupService.getDriverGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DriverGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出驾驶员分组 Excel")
    @PreAuthorize("@ss.hasPermission('dispatch:driver-group:export')")
    @OperateLog(type = EXPORT)
    public void exportDriverGroupExcel(@Valid DriverGroupPageReqVO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DriverGroupDO> list = driverGroupService.getDriverGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "驾驶员分组.xls", "数据", DriverGroupRespVO.class,
                BeanUtils.toBean(list, DriverGroupRespVO.class));
    }

    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取导游精简信息列表", description = "列表数据以树结构展示")
    public CommonResult<List<DriverGroupSimpleRespVO>> getSimpleGroupList(@RequestParam("isTree") String isTree) {
        List<DriverGroupDO> list = driverGroupService.getGroupList(
                SimpleListReqVO.builder()
                        .tenantId(TenantContextHolder.getTenantId())
                        .status(CommonStatusEnum.DISABLE.getStatus()).build());
        List<DriverGroupSimpleRespVO> respList = BeanUtils.toBean(list, DriverGroupSimpleRespVO.class);
        return success(respList);
    }

}