package com.younike.merchant;

import com.younike.common.annotation.Log;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.domain.AjaxResult;
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.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.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api("账单结算接口")
@RestController
@RequestMapping("/merchant/bil")
public class MerchantBilStatementController extends BaseController {

    @Autowired
    private IPersonalMerchantSettleService personalMerchantSettleService;


    /**
     * 账单结算分页列表
     */
    @ApiOperation("账单结算分页列表")
    @PreAuthorize("@ss.hasRole('merchant')")
    @GetMapping("/bilStatementList")
    public Object bilStatementList(PersonalMerchantSettleQueryParam queryParam) {
        if(ObjectUtils.isEmpty(queryParam) || queryParam.getMerchantId() == null){
            return AjaxResult.error("参数错误");
        }
        startPage();
        List<PersonalMerchantSettleVO> list = personalMerchantSettleService.bilStatementList(queryParam);
        return getDataTable(list);
    }

    /**
     * 账单可结算金额
     * @param queryParam
     * @return
     */
    @ApiOperation("账单结算金额信息")
    @PreAuthorize("@ss.hasRole('merchant')")
    @GetMapping("/getSettleAmount")
    public AjaxResult getPersonalMerchantSettleAmountInfo(PersonalMerchantSettleQueryParam queryParam) {
        if(ObjectUtils.isEmpty(queryParam) || queryParam.getMerchantId() == null){
            return AjaxResult.error("参数错误");
        }
        return AjaxResult.success(personalMerchantSettleService.getSettleAmount(queryParam));
    }


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