package com.jwipc.finance.controller.rest;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;
import com.jwipc.dto.ExportDataDTO;
import com.jwipc.finance.dto.ReceiveVoucherDTO;
import com.jwipc.finance.dto.StatementItemDTO;
import com.jwipc.finance.logic.AccountStatementApproveLogic;
import com.jwipc.finance.logic.AccountStatementDetailLogic;
import com.jwipc.finance.logic.AccountStatementLogic;
import com.jwipc.finance.model.query.*;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

/**
 * <p>
 *     对账模块-对账单 前端控制器
 * </p>
 *
 * @author JT
 * @since 2020-07-31
 */
@RestController
@RequestMapping(MODULE_ROOT + "/account/statement/detail/rest")
@RequiredArgsConstructor
public class AccountStatementDetailRestController {
    private final AccountStatementDetailLogic accountStatementDetailLogic;
    private final AccountStatementApproveLogic accountStatementApproveLogic;

    /**
     * 根据对账单ID获取对账单明细数据
     *
     * @param asId 对账单ID
     * @return 结果
     */
    @PostMapping("/find/by/statement/id")
    public ResponseEntity findByStatementId(Long asId){
        return ResponseEntity.ok(accountStatementDetailLogic.findByStatementIdPage(asId));
    }
    @AuditLogInter(value = "获取对账单明细数据", module = "对账单")
    @PostMapping("/find/by/statement/id/factoryCode")
    public ResponseEntity findByStatementIdAndFactoryCode(Long asId,String factoryCode,String removeLists){
        return ResponseEntity.ok(accountStatementDetailLogic.findByStatementIdAndFactoryCodePage(asId,factoryCode,removeLists));
    }

    @PostMapping("/find/by/statement/id/factoryCode/export")
    public void findByStatementIdAndFactoryCodeExport(HttpServletResponse response,String queryStr)  throws IOException {
        List<ExportDataDTO> exportDataDTOS = new ArrayList<>();
        //先加载固定字段
        exportDataDTOS.add(ExportDataDTO.builder().title("").field("factoryCode").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("").field("receiveId").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("").field("id").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("物料凭证号").field("mvCode").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("行号").field("receiveRowNo").build());
        exportDataDTOS.add(ExportDataDTO.builder().title("其他折扣").field("deductionRatio").color(IndexedColors.YELLOW.getIndex()).build());
        AccountStatementDetailQuery query = JSON.parseObject(queryStr,AccountStatementDetailQuery.class);
        PageView pageView = accountStatementDetailLogic.findByStatementIdAndFactoryCodePage(query.getAsId(),query.getFactoryCode(),null);
        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,"对账明细-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN),"sheet", pageView.getRows(), JSON.toJSONString(exportDataDTOS));
    }

    /**
     * 提供创建开票申请时获取对账单明细数据
     *
     * @param query 查询条件
     * @return 结果
     */
    @PostMapping("/provide/create/invoiced/apply")
    public ResponseEntity provideCreateInvoicedApply(CreateInvoicedApplyQuery query){
        return ResponseEntity.ok(accountStatementDetailLogic.provideCreateInvoicedApply(query));
    }

    /**
     * 快速创建开票申请的对账单明细列表
     * @param query 查询条件
     * @return 结果
     */
    @PostMapping("/fast/list")
    public ResponseEntity findByStatementId(AccountStatementDetailQuery query){
        // 快速创建开票申请对账明细列表默认查询数据状态为 9:对账单已确认
        query.setStatus(9);
        // 快速创建开票申请对账明细列表默认查询未开票的对账单明细行
        query.setIsComplete(0);
        if(LoginUtil.isSupplier()){
            query.setSupplierCode(LoginUtil.getAccount());
        }else {
            query.setUserId(LoginUtil.getUserId());
        }
        return ResponseEntity.ok(accountStatementDetailLogic.selectFastDataByQuery(query));
    }

    /**
     * 获取对账单审批确认备注
     *
     * @param query 查询参数
     * @return 结果
     */
    @PostMapping("/find/as/approve/log")
    public PageView findStatementApproveLog(AsApproveQuery query){
        return accountStatementApproveLogic.findByPage(query);
    }

    /**
     * 批量修改价格
     * @param ids 对账明细id
     * @param receiveIds 物料凭证ids
     */
    @PostMapping("/updatePrice")
    public ResponseEntity updatePrice(BigDecimal deductionRatio,String ids,String receiveIds,Long asId){
        accountStatementDetailLogic.updatePrice(deductionRatio,ids,receiveIds,asId);
        return ResponseEntity.ok(true);
    }

    @RequestMapping("/price/import")
    public ResponseEntity priceImport(@RequestPart("file") MultipartFile file){
        accountStatementDetailLogic.priceImport(file);
        return ResponseEntity.ok(true);
    }
}
