package com.yc.ship.module.otc.controller.admin.storescheduling;

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.module.otc.controller.admin.storescheduling.user.StoreSchedulingUserPageReqVO;
import com.yc.ship.module.otc.controller.admin.storescheduling.user.StoreSchedulingUserRespVO;
import com.yc.ship.module.otc.controller.admin.storescheduling.user.StoreSchedulingUserSaveReqVO;
import com.yc.ship.module.otc.convert.scheduling.SchedulingUserConvert;
import com.yc.ship.module.otc.dal.dataobject.storescheduling.StoreSchedulingDO;
import com.yc.ship.module.otc.dal.dataobject.storescheduling.StoreSchedulingUserDO;
import com.yc.ship.module.otc.service.storescheduling.StoreSchedulingService;
import com.yc.ship.module.otc.service.storescheduling.StoreSchedulingUserService;
import com.yc.ship.module.system.api.user.AdminUserApi;
import com.yc.ship.module.system.api.user.dto.AdminUserRespDTO;
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 java.util.Map;
import java.util.stream.Collectors;

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


@Tag(name = "管理后台 - 班次售票员")
@RestController
@RequestMapping("/store-scheduling-user")
@Validated
public class StoreSchedulingUserController {

    @Resource
    private AdminUserApi adminUserApi;

    @Resource
    private StoreSchedulingService storeSchedulingService;

    @Resource
    private StoreSchedulingUserService storeSchedulingUserService;

    @PostMapping("/create")
    @Operation(summary = "创建班次售票员")
    @PreAuthorize("@ss.hasPermission('otc:store-scheduling-user:create')")
    public CommonResult<Long> createStoreSchedulingUser(@Valid @RequestBody StoreSchedulingUserSaveReqVO createReqVO) {
        return success(storeSchedulingUserService.createStoreSchedulingUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新班次售票员")
    @PreAuthorize("@ss.hasPermission('otc:store-scheduling-user:update')")
    public CommonResult<Boolean> updateStoreSchedulingUser(@Valid @RequestBody StoreSchedulingUserSaveReqVO updateReqVO) {
        storeSchedulingUserService.updateStoreSchedulingUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除班次售票员")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('otc:store-scheduling-user:delete')")
    public CommonResult<Boolean> deleteStoreSchedulingUser(@RequestParam("id") Long id) {
        storeSchedulingUserService.deleteStoreSchedulingUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得班次售票员")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('otc:store-scheduling-user:query')")
    public CommonResult<StoreSchedulingUserRespVO> getStoreSchedulingUser(@RequestParam("id") Long id) {
        StoreSchedulingUserDO storeSchedulingUser = storeSchedulingUserService.getStoreSchedulingUser(id);
        return success(BeanUtils.toBean(storeSchedulingUser, StoreSchedulingUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得班次售票员分页")
    @PreAuthorize("@ss.hasPermission('otc:store-scheduling-user:query')")
    public CommonResult<PageResult<StoreSchedulingUserRespVO>> getStoreSchedulingUserPage(@Valid StoreSchedulingUserPageReqVO pageReqVO) {
        PageResult<StoreSchedulingUserDO> pageResult = storeSchedulingUserService.getStoreSchedulingUserPage(pageReqVO);
        List<StoreSchedulingUserRespVO> list = BeanUtils.toBean(pageResult.getList(), StoreSchedulingUserRespVO.class);

        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(
                convertList(list, StoreSchedulingUserRespVO::getUserId).stream().distinct().collect(Collectors.toList()));
        Map<Long, StoreSchedulingDO> schedulingMap = storeSchedulingService.getSchedulingMap(
                convertList(list, StoreSchedulingUserRespVO::getSchedulingId));
        list = SchedulingUserConvert.INSTANCE.convertRespListByUser(list, userMap);
        list = SchedulingUserConvert.INSTANCE.convertRespListByScheduling(list, schedulingMap);
        return success(new PageResult<>(list, pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出班次售票员 Excel")
    @PreAuthorize("@ss.hasPermission('otc:store-scheduling-user:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportStoreSchedulingUserExcel(@Valid StoreSchedulingUserPageReqVO pageReqVO,
                                               HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreSchedulingUserDO> list = storeSchedulingUserService.getStoreSchedulingUserPage(pageReqVO).getList();

        List<StoreSchedulingUserRespVO> respList = BeanUtils.toBean(list, StoreSchedulingUserRespVO.class);
        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(
                convertList(respList, StoreSchedulingUserRespVO::getUserId));
        Map<Long, StoreSchedulingDO> schedulingMap = storeSchedulingService.getSchedulingMap(
                convertList(respList, StoreSchedulingUserRespVO::getSchedulingId));
        respList = SchedulingUserConvert.INSTANCE.convertRespListByUser(respList, userMap);
        respList = SchedulingUserConvert.INSTANCE.convertRespListByScheduling(respList, schedulingMap);
        // 导出 Excel
        ExcelUtils.write(response, "班次售票员.xls", "数据", StoreSchedulingUserRespVO.class, respList);
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取站点精简信息列表", description = "只包含被启用的站点，主要用于前端的下拉选项")
    public CommonResult<List<StoreSchedulingUserRespVO>> getSimpleSchedulingUserList() {
        List<StoreSchedulingUserDO> list = storeSchedulingUserService.getSchedulingUserList();
        return success(BeanUtils.toBean(list, StoreSchedulingUserRespVO.class));
    }

}
