package com.younike.admin;

import com.younike.common.annotation.Log;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.domain.AjaxResult;
import com.younike.common.core.page.TableDataInfo;
import com.younike.common.enums.BusinessType;
import com.younike.common.utils.poi.ExcelUtil;
import com.younike.system.domain.vo.PersonalMerchantSettleVO;
import com.younike.system.param.PersonalMerchantOrderSettleParam;
import com.younike.system.param.PersonalMerchantSettleQueryParam;
import com.younike.system.service.IPersonalMerchantSettleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 个体户账单结算Controller
 * @Author dail
 * @Date 2022/3/3
 */
@Api("个体户账单结算接口")
@RestController
@RequestMapping("/admin/perSettle")
public class AdminPersonalMerchantSettleController extends BaseController {

    @Autowired
    private IPersonalMerchantSettleService personalMerchantSettleService;


    /**
     * 个体户账单结算分页列表
     */
    @ApiOperation("个体户账单结算分页列表")
    @PreAuthorize("@ss.hasRole('operation')")
    @GetMapping("/personalMerchantSettleList")
    public TableDataInfo listPersonalMerchantSettle(PersonalMerchantSettleQueryParam queryParam) {
        startPage();
        List<PersonalMerchantSettleVO> list = personalMerchantSettleService.listPersonalMerchantSettle(queryParam);
        return getDataTable(list);
    }

    /**
     * 个体户账单结算金额信息
     */
    @ApiOperation("个体户账单结算金额信息")
    @PreAuthorize("@ss.hasRole('operation')")
    @GetMapping("/getPersonalSettleAmount")
    public AjaxResult getPersonalMerchantSettleAmountInfo() {
        return AjaxResult.success(personalMerchantSettleService.getPersonalMerchantSettleAmountInfo());
    }

    /**
     * 个体户账单结算
     */
    @ApiOperation("个体户账单结算接口")
    @PreAuthorize("@ss.hasRole('operation')")
    @PostMapping("/settleOrder")
    public AjaxResult settlePersonalMerchantOrder(@RequestBody PersonalMerchantOrderSettleParam orderSettleParam) {
        // 参数校验
        if (CollectionUtils.isEmpty(orderSettleParam.getPersonalMerchantSettleList())) {
            return AjaxResult.error("个体户账单结算列表不能为空!");
        }
        if (orderSettleParam.getResourceUrl() == null || StringUtils.isBlank(orderSettleParam.getResourceUrl().trim())) {
            return AjaxResult.error("结算资源url不能为空!");
        }
        return personalMerchantSettleService.settlePersonalMerchantOrder(orderSettleParam);
    }

    /**
     * 查询个体户账单结算资源
     */
    @ApiOperation("查询个体户账单结算资源")
    @PreAuthorize("@ss.hasRole('operation')")
    @GetMapping("/getPersonalSettleResource")
    public AjaxResult getPersonalSettleResource(Long personalMerchantSettleId) {
        if (personalMerchantSettleId == null) {
            return AjaxResult.error("参数错误(个体户账单结算id不能为空)");
        }
        return personalMerchantSettleService.getPersonalSettleResource(personalMerchantSettleId);
    }

    /**
     * 导出个体户账单结算列表
     */
    @ApiOperation("导出个体户账单结算列表")
    @PreAuthorize("@ss.hasRole('operation')")
    @Log(title = "个体户账单列表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PersonalMerchantSettleQueryParam queryParam) {
        List<PersonalMerchantSettleVO> list = personalMerchantSettleService.listPersonalMerchantSettle(queryParam);
        ExcelUtil<PersonalMerchantSettleVO> util = new ExcelUtil<>(PersonalMerchantSettleVO.class);
        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.error("导出数据不可为空！");
        }
        return util.exportExcelAndUpload(list, "个体户账单结算列表");
    }

}
