package cn.iocoder.yudao.module.member.controller.admin.customertraffic;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.customertraffic.vo.CustomerTrafficPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.customertraffic.vo.CustomerTrafficRespVO;
import cn.iocoder.yudao.module.member.controller.admin.customertraffic.vo.CustomerTrafficSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.customertraffic.CustomerTrafficDO;
import cn.iocoder.yudao.module.member.service.customertraffic.CustomerTrafficService;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 顾客流量")
@RestController
@RequestMapping("/member/customer-traffic")
@Validated
public class CustomerTrafficController {

    @Resource
    private CustomerTrafficService customerTrafficService;

    @PostMapping("/create")
    @Operation(summary = "创建顾客流量")
    @PreAuthorize("@ss.hasPermission('member:customer-traffic:create')")
    public CommonResult<Long> createCustomerTraffic(@Valid @RequestBody CustomerTrafficSaveReqVO createReqVO) {
        return success(customerTrafficService.createCustomerTraffic(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新顾客流量")
    @PreAuthorize("@ss.hasPermission('member:customer-traffic:update')")
    public CommonResult<Boolean> updateCustomerTraffic(@Valid @RequestBody CustomerTrafficSaveReqVO updateReqVO) {
        customerTrafficService.updateCustomerTraffic(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除顾客流量")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:customer-traffic:delete')")
    public CommonResult<Boolean> deleteCustomerTraffic(@RequestParam("id") Long id) {
        customerTrafficService.deleteCustomerTraffic(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得顾客流量")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:customer-traffic:query')")
    public CommonResult<CustomerTrafficRespVO> getCustomerTraffic(@RequestParam("id") Long id) {
        CustomerTrafficDO customerTraffic = customerTrafficService.getCustomerTraffic(id);
        return success(BeanUtils.toBean(customerTraffic, CustomerTrafficRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得顾客流量分页")
    @PreAuthorize("@ss.hasPermission('member:customer-traffic:query')")
    public CommonResult<PageResult<CustomerTrafficRespVO>> getCustomerTrafficPage(@Valid CustomerTrafficPageReqVO pageReqVO) {
        PageResult<CustomerTrafficDO> pageResult = customerTrafficService.getCustomerTrafficPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CustomerTrafficRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出顾客流量 Excel")
    @PreAuthorize("@ss.hasPermission('member:customer-traffic:export')")
    @OperateLog(type = EXPORT)
    public void exportCustomerTrafficExcel(@Valid CustomerTrafficPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CustomerTrafficDO> list = customerTrafficService.getCustomerTrafficPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "顾客流量.xls", "数据", CustomerTrafficRespVO.class,
                        BeanUtils.toBean(list, CustomerTrafficRespVO.class));
    }



}