package com.zt.plat.module.base.controller.admin.base;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.base.controller.admin.base.vo.ContactPageReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.ContactRespVO;
import com.zt.plat.module.base.controller.admin.base.vo.ContactSaveReqVO;
import com.zt.plat.module.base.dal.dataobject.base.ContactDO;
import com.zt.plat.module.base.service.base.ContactService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 联系人信息")
@RestController
@RequestMapping("/base/contact")
@Validated
public class ContactController {


    @Resource
    private ContactService contactService;

    @PostMapping("/create")
    @Operation(summary = "创建联系人信息")
    //@PreAuthorize("@ss.hasPermission('basic:contact-config:create')")
    public CommonResult<ContactRespVO> createContact(@Valid @RequestBody ContactSaveReqVO createReqVO) {
        return success(contactService.createContact(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新联系人信息")
    //@PreAuthorize("@ss.hasPermission('basic:contact-config:update')")
    public CommonResult<Boolean> updateContact(@Valid @RequestBody ContactSaveReqVO updateReqVO) {
        contactService.updateContact(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除联系人信息")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('basic:contact-config:delete')")
    public CommonResult<Boolean> deleteContact(@RequestParam("id") Long id) {
        contactService.deleteContact(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除联系人信息")
    //@PreAuthorize("@ss.hasPermission('basic:contact-config:delete')")
    public CommonResult<Boolean> deleteContactList(@RequestBody BatchDeleteReqVO req) {
        contactService.deleteContactListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得联系人信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:contact:query')")
    public CommonResult<ContactRespVO> getContact(@RequestParam("id") Long id) {
        ContactDO contact = contactService.getContact(id);
        return success(BeanUtils.toBean(contact, ContactRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得联系人信息分页")
    @PreAuthorize("@ss.hasPermission('base:contact:query')")
    public CommonResult<PageResult<ContactRespVO>> getContactPage(@Valid ContactPageReqVO pageReqVO) {
        PageResult<ContactDO> pageResult = contactService.getContactPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ContactRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出联系人信息 Excel")
    @PreAuthorize("@ss.hasPermission('base:contact:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportContactExcel(@Valid ContactPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContactDO> list = contactService.getContactPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "联系人信息.xls", "数据", ContactRespVO.class,
                        BeanUtils.toBean(list, ContactRespVO.class));
    }

    @PutMapping("/enable-list")
    @Operation(summary = "批量更新")
    //@PreAuthorize("@ss.hasAnyPermissions({'base:contact:update','basic:contact-config:disable','basic:contact-config:enable'})")
    public CommonResult<Boolean> enableContactList(@RequestBody List<ContactSaveReqVO> saveReqVOS) {
        contactService.enableContactList(saveReqVOS);
        return success(true);
    }
}