package cn.newfeifan.mall.module.distri.controller.admin.dailybill;

import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
import cn.newfeifan.mall.framework.excel.core.util.ExcelUtils;
import cn.newfeifan.mall.framework.operatelog.core.annotations.OperateLog;
import cn.newfeifan.mall.module.distri.controller.admin.dailybill.excelvo.DailyBillExcelRespVO;
import cn.newfeifan.mall.module.distri.controller.admin.dailybill.vo.DailyBillPageReqVO;
import cn.newfeifan.mall.module.distri.controller.admin.dailybill.vo.DailyBillRespVO;
import cn.newfeifan.mall.module.distri.controller.admin.dailybill.vo.DailyBillSaveReqVO;
import cn.newfeifan.mall.module.distri.convert.dailybill.DailyBillConvert;
import cn.newfeifan.mall.module.distri.dal.dataobject.dailybill.DailyBillDO;
import cn.newfeifan.mall.module.distri.service.dailybill.DailyBillService;
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 cn.newfeifan.mall.framework.common.pojo.CommonResult.success;
import static cn.newfeifan.mall.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 每日账单")
@RestController
@RequestMapping("/distri/daily-bill")
@Validated
public class DailyBillController {

    @Resource
    private DailyBillService dailyBillService;

    @PostMapping("/create")
    @Operation(summary = "创建每日账单")
    @PreAuthorize("@ss.hasPermission('distri:daily-bill:create')")
    public CommonResult<Long> createDailyBill(@Valid @RequestBody DailyBillSaveReqVO createReqVO) {
        return success(dailyBillService.createDailyBill(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新每日账单")
    @PreAuthorize("@ss.hasPermission('distri:daily-bill:update')")
    public CommonResult<Boolean> updateDailyBill(@Valid @RequestBody DailyBillSaveReqVO updateReqVO) {
        dailyBillService.updateDailyBill(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除每日账单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:daily-bill:delete')")
    public CommonResult<Boolean> deleteDailyBill(@RequestParam("id") Long id) {
        dailyBillService.deleteDailyBill(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得每日账单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:daily-bill:query')")
    public CommonResult<DailyBillRespVO> getDailyBill(@RequestParam("id") Long id) {
        DailyBillDO dailyBill = dailyBillService.getDailyBill(id);
        return success(BeanUtils.toBean(dailyBill, DailyBillRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得每日账单分页")
    @PreAuthorize("@ss.hasPermission('distri:daily-bill:query')")
    public CommonResult<PageResult<DailyBillRespVO>> getDailyBillPage(@Valid DailyBillPageReqVO pageReqVO) {
        PageResult<DailyBillRespVO> pageResult = dailyBillService.getDailyBillPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出每日账单 Excel")
    @PreAuthorize("@ss.hasPermission('distri:daily-bill:export')")
    @OperateLog(type = EXPORT)
    public void exportDailyBillExcel(@Valid DailyBillPageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DailyBillExcelRespVO> list = DailyBillConvert.INSTANCE.convertList(dailyBillService.getDailyBillPage(pageReqVO).getList());
        // 导出 Excel
        ExcelUtils.write(response, "每日账单.xls", "数据", DailyBillExcelRespVO.class,
                list);
    }

    @GetMapping("/CalcDailyBill")
    @Operation(summary = "计算每日账单")
    @PreAuthorize("@ss.hasPermission('distri:daily-bill:delete')")
    public CommonResult<String> getMerchantDailyBill() {
        dailyBillService.caleDailyBill();
        return success("success");
    }

}