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

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.link.vo.LinkPageReqVO;
import com.yc.ship.module.otc.controller.admin.link.vo.LinkRespVO;
import com.yc.ship.module.otc.controller.admin.link.vo.LinkSaveReqVO;
import com.yc.ship.module.otc.dal.dataobject.link.LinkDO;
import com.yc.ship.module.otc.dal.dataobject.link.LinkDetailDO;
import com.yc.ship.module.otc.service.link.LinkDetailService;
import com.yc.ship.module.otc.service.link.LinkService;
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.common.util.collection.CollectionUtils.convertList;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 链接管理")
@RestController
@RequestMapping("/link")
@Validated
public class LinkController {

    @Resource
    private LinkService linkService;

    @Resource
    private LinkDetailService linkDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建链接管理")
    @PreAuthorize("@ss.hasPermission('otc:link:create')")
    public CommonResult<Long> createLink(@Valid @RequestBody LinkSaveReqVO createReqVO) {
        return success(linkService.createLink(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新链接管理")
    @PreAuthorize("@ss.hasPermission('otc:link:update')")
    public CommonResult<Boolean> updateLink(@Valid @RequestBody LinkSaveReqVO updateReqVO) {
        linkService.updateLink(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除链接管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('otc:link:delete')")
    public CommonResult<Boolean> deleteLink(@RequestParam("id") Long id) {
        linkService.deleteLink(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得链接管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('otc:link:query')")
    public CommonResult<LinkRespVO> getLink(@RequestParam("id") Long id) {
        LinkDO link = linkService.getLink(id);
        LinkRespVO respVO = BeanUtils.toBean(link, LinkRespVO.class);
        List<LinkDetailDO> linkDetails = linkDetailService.getLinkDetailByLinkId(id);
        if (respVO.getIsMultiple() == 1) {
            respVO.setCheckedValues(convertList(linkDetails, LinkDetailDO::getSourceCode));
        } else {
            respVO.setCheckedValue(linkDetails.get(0).getSourceCode());
        }
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得链接管理分页")
//    @PreAuthorize("@ss.hasPermission('otc:link:query')")
    public CommonResult<PageResult<LinkRespVO>> getLinkPage(@Valid LinkPageReqVO pageReqVO) {
        PageResult<LinkDO> pageResult = linkService.getLinkPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LinkRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出链接管理 Excel")
    @PreAuthorize("@ss.hasPermission('otc:link:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportLinkExcel(@Valid LinkPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LinkDO> list = linkService.getLinkPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "链接管理.xls", "数据", LinkRespVO.class,
                BeanUtils.toBean(list, LinkRespVO.class));
    }

}
