package com.ruoyi.web.controller.shipping.finance;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.basicData.domain.TCorps;
import com.ruoyi.basicData.domain.TCustomerContact;
import com.ruoyi.basicData.mapper.TCorpsMapper;
import com.ruoyi.basicData.mapper.TCustomerContactMapper;
import com.ruoyi.basicData.service.impl.TCorpsServiceImpl;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.finance.domain.TFee;
import com.ruoyi.finance.domain.TWareHouseFees;
import com.ruoyi.finance.excel.Charge;
import com.ruoyi.finance.service.ITFeeDoService;
import com.ruoyi.finance.service.ITFeeService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.shipping.domain.TAccount;
import com.ruoyi.shipping.domain.TFeeInvoice;
import com.ruoyi.shipping.excel.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @Author ZhaoQian La
 * @Date 2021/5/7 11:49
 * @Version 1.0
 */

/**
 * 网页版对账
 */
@RestController
@RequestMapping("/financial/webVersionAccount")
public class webVersionAccount extends BaseController {
    @Autowired
    private ITFeeService tFeeService;
    @Autowired
    private ITFeeDoService tFeeDoService;
    @Autowired
    private TCorpsMapper tCorpsMapper;

    /**
     * 网页版对账列表数据查询
     * @param tFee
     * @return
     */
    @Log(title = "网页版对账查询", businessType = BusinessType.INSERT)
    @GetMapping("/webVersionChargeList")
    public TableDataInfo webVersionChargeList(TFee tFee) {
        startPage();
        tFee.setfBilltype("KHDZ");
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        tFee.setCreateBy(loginUser.getUser().getUserName());
        List<Map<String, Object>> list = tFeeService.webVersionTFee(tFee);
        return getDataTable(list);
    }

    /**
     * 网页版对账申请提交
     * @param tFee
     * @param tFeeDo
     * @param billsType
     * @return
     */
    @Log(title = "网页版对账申请", businessType = BusinessType.INSERT)
    @PostMapping(value = "/webVersionAdd")
    @RepeatSubmit
    public AjaxResult webVersionAdd(@RequestParam("tFee") String tFee,
                                    @RequestParam("tFeeDo") String tFeeDo,
                                    @RequestParam(value = "billsType", required = false) String billsType) {
        if (StringUtils.isEmpty(billsType)) {
            billsType = "KHDZ";
        }
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tFeeService.insertWebVersion(tFee, tFeeDo, loginUser, billsType);
    }

    /**
     * 网页版对账审核提交
     * @param fIds
     * @return
     */
    @Log(title = "网页版对账", businessType = BusinessType.INSERT)
    @GetMapping(value = "/{fIds}")
    @RepeatSubmit
    public AjaxResult webVersionConfirm(@PathVariable Long[] fIds) {
        // 获取当前的用户
        if (fIds.length < 1) {
            return AjaxResult.error("请选择对账单后再确认对账");
        }
        String billsType = "KHDZ";
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tFeeService.webVersionConfirm(fIds, billsType, loginUser);
    }
    /**
     * 网页版查询对账数据
     * @param tAccount
     * @return
     */
    @Log(title = "网页版对账数据", businessType = BusinessType.INSERT)
    @GetMapping("/webVersionList")
    public TableDataInfo webVersionList(TAccount tAccount) {
        //tFee.setfBilltype("KHDZ");
        startPage();
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        tAccount.setCreateBy(loginUser.getUser().getUserName());
        List<Map<String, Object>> list = tFeeService.webVersionList(tAccount);
        return getDataTable(list);
    }

    /**
     * 网页版查询对账明细
     * @param tAccount
     * @return
     */
    //@Log(title = "网页版对账查询明细", businessType = BusinessType.INSERT)
    @PostMapping("/webVersionDetail")
    public TableDataInfo webVersionDetail(@RequestBody(required = false) TAccount tAccount) {
        //tFee.setfBilltype("KHDZ");
        if (tAccount == null){
            tAccount = new TAccount();
        }
        startPage();
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        tAccount.setCreateBy(loginUser.getUser().getUserName());
        List<Map<String, Object>> list = tFeeService.webVersionDetail(tAccount);
        return getDataTable(list);
    }

    /**
     *网页版查询发票
     * @param tFee
     * @return
     */
    @GetMapping("/webVersionInvoice")
    public TableDataInfo webVersionInvoice(TFee tFee) {
        //tFee.setfBilltype("KHDZ");
        startPage();
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        tFee.setCreateBy(loginUser.getUser().getUserName());
        List<Map<String, Object>> list = tFeeService.webVersionInvoice(tFee);
        return getDataTable(list);
    }

    /**
     * 网页版查询发票明细
     * @param tFee
     * @return
     */
    @Log(title = "网页版查询发票明细", businessType = BusinessType.INSERT)
    @PostMapping("/webVersionInvoiceDetail")
    public TableDataInfo webVersionInvoiceDetail(@RequestBody TFee tFee) {
        //tFee.setfBilltype("KHDZ");
        startPage();
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        tFee.setCreateBy(loginUser.getUser().getUserName());
        List<Map<String, Object>> list = tFeeService.webVersionInvoiceDetail(tFee);
        return getDataTable(list);
    }

    @Log(title = "网页版发票申请删除", businessType = BusinessType.INSERT)
    @DeleteMapping("/remove/{fIds}")
    public AjaxResult remove(@PathVariable Long[] fIds) {
        return tFeeService.deleteWebVersionByIds(fIds);
    }

    /**
     * 网页版提交发票申请
     * @param tFee
     * @return
     */
    @Log(title = "网页版发票申请保存", businessType = BusinessType.INSERT)
    @PostMapping("/webVersionApplyFP")
    public AjaxResult webVersionApplyFP(@RequestBody TFee tFee) {
        if (tFee == null){
            return AjaxResult.error("请选择对账单");
        }
        if (StringUtils.isEmpty(tFee.getfInvoiceRise())){
            return AjaxResult.error("发票抬头不能为空");
        }
        if (tFee.gettFeeInvoice()== null){
            return AjaxResult.error("开票信息不能为空");
        }
        String billType = "ApplyFP";
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tFeeService.webVersionApplyFP(tFee,loginUser,billType,tFee.gettFeeInvoice());
    }
    /**
     * 网页版修改发票申请
     * @param tFee
     * @return
     */
    //@Log(title = "网页版发票申请保存", businessType = BusinessType.INSERT)
    @PostMapping("/webVersionUpdateApplyFP")
    public AjaxResult webVersionUpdateApplyFP(@RequestBody TFee tFee) {
        if (tFee.getfId() == null){
            return AjaxResult.error("主表id不能为空");
        }
        if (tFee.gettFeeInvoice() == null){
            return AjaxResult.error("开票信息不能为空");
        }
        String billType = "ApplyFP";
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tFeeService.webVersionUpdateApplyFP(tFee,loginUser,billType);
    }

    /**
     * 网页版提交发票审核
     * @param tFee
     * @param
     * @return
     */
    @Log(title = "网页版发票提交审核", businessType = BusinessType.INSERT)
    @PostMapping("/webVersionSubmitApplyFP")
    public AjaxResult webVersionSubmitApplyFP(@RequestBody TFee tFee) {
        if (tFee == null){
            return AjaxResult.error("请选择发票申请");
        }
        String billType = "ApplyFP";
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tFeeService.webVersionSubmitApplyFP(tFee,loginUser,billType);
    }

    /**
     * 网页版申请发票查询客户信息
     * @param tCorps
     * @return
     */
    @PostMapping("/getClientMessage")
    public AjaxResult getClientMessage(TCorps tCorps){
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        tCorps.setfTel(loginUser.getUser().getPhonenumber());
        return AjaxResult.success("success",tCorpsMapper.getClientMessage(tCorps));
    }
    /**
     * 导出财务数据主列表
     */
    //@PreAuthorize("@ss.hasPermi('finance:charge:export')")
    @Log(title = "财务数据主", businessType = BusinessType.EXPORT)
    //@DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/export")
    public AjaxResult export(TFee tFee) throws Exception {
        tFee.setfBilltype("KHDZ");
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        tFee.setCreateBy(loginUser.getUser().getUserName());
        List<Account> list = tFeeService.accountExport(tFee);
        ExcelUtil<Account> util = new ExcelUtil<Account>(Account.class);
        return util.exportExcel(list, "对账单");
    }


}
