package com.jwipc.finance.controller.rest;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.jwipc.finance.dto.CreatePcaDetailDto;
import com.jwipc.finance.dto.PaymentCodApplySaveDTO;
import com.jwipc.finance.logic.PaymentAdvanceApplyLogic;
import com.jwipc.finance.logic.PaymentCodApplyLogic;
import com.jwipc.finance.model.query.PaymentApplyHeadQuery;
import com.jwipc.finance.model.query.PaymentApplyItemQuery;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.dto.RoleListDTO;
import com.jwipc.sys.logic.RoleLogic;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.jwipc.finance.constant.ModuleConst.MODULE_ROOT;

/**
 * <p>
 * 对账付款申请主表 前端控制器
 * </p>
 *
 * @author WT
 * @since 2024-03-08
 */
@RestController
@RequestMapping(MODULE_ROOT + "/payment/cod/apply/rest")
@RequiredArgsConstructor
public class PaymentCodApplyRestController {

    private final PaymentCodApplyLogic paymentCodApplyLogic;

    private final RoleLogic roleLogic;
    /**
     * 查询预付款申请首页列表数据
     *
     * @param query 查询条件
     * @return 结果
     */
    @AuditLogInter(value = "首页查询列表", module = "对账付款申请")
    @RequestMapping("/list")
    public PageView indexPageTableData(PaymentApplyHeadQuery query) {
        if (LoginUtil.isSupplier()){
            query.setSupplierCode(LoginUtil.getAccount());
        }else{
            boolean flag = true;
            List<RoleListDTO> userRoles = roleLogic.getUserRoles(LoginUtil.getUserId());
            if(CollUtil.isNotEmpty(userRoles)){
                for (RoleListDTO userRole : userRoles) {
                    if("query_all_data".equals(userRole.getRolekey())){
                        flag = false;
                    }
                }
            }
            if(flag){
                query.setUserId(LoginUtil.getUserId());
            }
        }
        return paymentCodApplyLogic.selectDataByQuery(query);
    }

    /**
     * 查询对账付款申请发票数据
     *
     * @param query 查询条件
     * @return 结果
     */
    @PostMapping("/query/cod/apply/invoice")
    public ResponseEntity queryCodApplyInvoice(PaymentApplyItemQuery query) {
        List<CreatePcaDetailDto> pcaDetailList = paymentCodApplyLogic.readCodPaymentInvoiceData(query);
        return ResponseEntity.ok(pcaDetailList);
    }

    /**
     * 查询对账付款申请发票数据
     *
     * @param id 查询条件
     * @return 结果
     */
    @PostMapping("/query/cod/apply/invoice/id")
    public ResponseEntity queryCodApplyInvoiceById(Long id,Integer totalFlag,String businessType) {
        List<CreatePcaDetailDto> pcaDetailList = paymentCodApplyLogic.queryCodApplyInvoiceById(id,totalFlag,businessType);
        return ResponseEntity.ok(pcaDetailList);
    }

    /**
     * 查询对账付款申请发票数据
     *
     * @param accountStatementNo 查询条件
     * @return 结果
     */
    @PostMapping("/query/cod/apply/invoice/accountStatementNo")
    public ResponseEntity queryCodApplyInvoiceByAccountStatementNo(String accountStatementNo) {
        List<CreatePcaDetailDto> pcaDetailList = paymentCodApplyLogic.queryCodApplyInvoiceByAccountStatementNo(accountStatementNo);
        return ResponseEntity.ok(pcaDetailList);
    }



    /**
     * 数据保存
     * fin/payment/cod/apply/rest/dave/save
     *
     * @param data 待保存的数据
     * @return 结果
     */
    @PostMapping("/dave/saveOrUpdate")
    public ResponseEntity dataSave(@RequestBody PaymentCodApplySaveDTO data) throws Exception {

        paymentCodApplyLogic.saveOrUpdate(data);
        return ResponseEntity.ok(true);
    }

    @PostMapping("/dave/saveOrUpdateCheck")
    public ResponseEntity dataSaveCheck(@RequestBody PaymentCodApplySaveDTO data) throws Exception {
        return ResponseEntity.ok(paymentCodApplyLogic.saveOrUpdateCheck(data));
    }

    /**
     * 删除对账付款申请单
     *
     * @param applyIds 申请ID
     * @return 结果
     */
    @PostMapping("/data/del")
    public ResponseEntity dataDel(String applyIds) {
        paymentCodApplyLogic.deleteCodApply(applyIds);
        return ResponseEntity.ok(true);
    }

    /**
     * 提交OA
     * @param id 付款id
     */
    @PostMapping("/clear/oaNumber")
    public ResponseEntity clearOaNumber(String ids){
        if(StringUtils.isEmpty(ids)){
            return ResponseEntity.ok(false);
        }
        paymentCodApplyLogic.clearOaNumber(ids);

        return ResponseEntity.ok(true);
    }

    /**
     * 提交OA
     * @param id 付款id
     */
    @PostMapping("/push/oa")
    public ResponseEntity pushOa(String ids){
//        paymentCodApplyLogic.pushOa(ids);
        paymentCodApplyLogic.pushOpen(ids);

        return ResponseEntity.ok(true);
    }

    /**
     * 发送邮件催收
     *
     * @param id id
     */
    @PostMapping("/sendEmailCollection")
    public ResponseEntity sendEmailCollection(Long id) {
        paymentCodApplyLogic.sendEmailCollection(id);
        return ResponseEntity.ok(true);
    }
}
