package com.bs.cw.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.bs.jxc.domain.CmCust;
import com.bs.jxc.service.ICmCustService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bs.common.annotation.Log;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.hutool.core.lang.Validator;
import com.bs.cw.domain.CwFinanceCash;
import com.bs.cw.service.ICwFinanceCashService;
import javax.annotation.Resource;

/**
 * 现金日记账Controller
 *
 * @author bs
 * @date 2024-02-07
 */
@Api(tags = "现金日记账")
@RestController
@RequestMapping("/finance/cash")
public class CwFinanceCashController extends BaseController {
    @Resource
    private ICwFinanceCashService cwFinanceCashService;
    @Resource
    private ICmCustService cmCustService;

    /**
     * 分页查询现金日记账列表
     */
    @ApiOperation("分页查询现金日记账列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CwFinanceCash cwFinanceCash) {
        startPage();
        LambdaQueryWrapper<CwFinanceCash> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cwFinanceCash);
        List<CwFinanceCash> list = cwFinanceCashService.list(queryWrapper);
        List<CmCust> lsCust = cmCustService.list();
        Map<Long, String> custMap = new HashMap<>();
        lsCust.forEach(ele -> {
            custMap.put(ele.getId(), ele.getCustName());
        });
        list.forEach(ele -> {
            if (null != ele.getCustId()) {
                ele.setCustName(custMap.get(ele.getCustId()));
            }
        });
        return getDataTable(list);
    }

    /**
     * 查询现金日记账列表
     */
    @ApiOperation("查询现金日记账列表")
    @GetMapping("/list")
    public AjaxResult list(CwFinanceCash cwFinanceCash) {
        LambdaQueryWrapper<CwFinanceCash> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cwFinanceCash);
        List<CwFinanceCash> list = cwFinanceCashService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出现金日记账列表
     */
    @ApiOperation("导出现金日记账列表")
    @Log(title = "现金日记账导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CwFinanceCash cwFinanceCash) {
        LambdaQueryWrapper<CwFinanceCash> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cwFinanceCash);
        List<CwFinanceCash> list = cwFinanceCashService.list(queryWrapper);
        ExcelUtil<CwFinanceCash> util = new ExcelUtil<CwFinanceCash>(CwFinanceCash. class);
        util.exportExcel(response, list, "现金日记账数据");
    }

    /**
     * 获取现金日记账详细信息
     */
    @ApiOperation("获取现金日记账详细信息")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        CwFinanceCash cash = cwFinanceCashService.getById(ID);
        if (null != cash.getCustId()) {
            CmCust cust = cmCustService.getById(cash.getCustId());
            if (null != cust) {
                cash.setCustName(cust.getCustName());
            }
        }
        return success(cash);
    }

    /**
     * 新增现金日记账
     */
    @ApiOperation("新增现金日记账")
    @Log(title = "现金日记账新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CwFinanceCash cwFinanceCash) {
        cwFinanceCash.setHxAmount(new BigDecimal(0));
        cwFinanceCash.setWhxAmount(cwFinanceCash.getPaidMoney());
        return toAjax(cwFinanceCashService.save(cwFinanceCash));
    }

    /**
     * 修改现金日记账
     */
    @ApiOperation("修改现金日记账")
    @Log(title = "现金日记账修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CwFinanceCash cwFinanceCash) {
        if (cwFinanceCash.getHxAmount().compareTo(new BigDecimal(0)) == 0) {
            cwFinanceCash.setWhxAmount(cwFinanceCash.getPaidMoney());
        } else {
            return warn("已存在核金额，不能进行修改！");
        }
        return toAjax(cwFinanceCashService.updateById(cwFinanceCash));
    }

    /**
     * 删除现金日记账
     */
    @ApiOperation("删除现金日记账")
    @Log(title = "现金日记账删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable List<Long> IDs) {
        return toAjax(cwFinanceCashService.removeBatchByIds(IDs));
    }

    /**
      * 条件设置
      */
    private void condition (LambdaQueryWrapper<CwFinanceCash> queryWrapper,CwFinanceCash cwFinanceCash){


        //id
        if(Validator.isNotEmpty(cwFinanceCash.getId())){
            queryWrapper.eq(CwFinanceCash::getId,cwFinanceCash.getId());
        }

        //关联ID
        if(Validator.isNotEmpty(cwFinanceCash.getRelId())){
            queryWrapper.eq(CwFinanceCash::getRelId,cwFinanceCash.getRelId());
        }

        //关联ID2
        if(Validator.isNotEmpty(cwFinanceCash.getRelId2())){
            queryWrapper.eq(CwFinanceCash::getRelId2,cwFinanceCash.getRelId2());
        }

        //单据号
        if(Validator.isNotEmpty(cwFinanceCash.getBillNo())){
            queryWrapper.like(CwFinanceCash::getBillNo,cwFinanceCash.getBillNo());
        }

        //付款单位
        if(Validator.isNotEmpty(cwFinanceCash.getCustId())){
            queryWrapper.eq(CwFinanceCash::getCustId,cwFinanceCash.getCustId());
        }

        //币别
        if(Validator.isNotEmpty(cwFinanceCash.getCurrencyCode())){
            queryWrapper.eq(CwFinanceCash::getCurrencyCode,cwFinanceCash.getCurrencyCode());
        }

        //实收金额
        if(Validator.isNotEmpty(cwFinanceCash.getPaidMoney())){
            queryWrapper.eq(CwFinanceCash::getPaidMoney,cwFinanceCash.getPaidMoney());
        }

        //已核销金额
        if(Validator.isNotEmpty(cwFinanceCash.getHxAmount())){
            queryWrapper.eq(CwFinanceCash::getHxAmount,cwFinanceCash.getHxAmount());
        }

        //未核金额
        if(Validator.isNotEmpty(cwFinanceCash.getWhxAmount())){
            queryWrapper.eq(CwFinanceCash::getWhxAmount,cwFinanceCash.getWhxAmount());
        }

        //处理金额
        if(Validator.isNotEmpty(cwFinanceCash.getHandleAmount())){
            queryWrapper.eq(CwFinanceCash::getHandleAmount,cwFinanceCash.getHandleAmount());
        }

        //实收时间
        if(Validator.isNotEmpty(cwFinanceCash.getBeginDate())){
            queryWrapper.ge(CwFinanceCash::getPaidTime,cwFinanceCash.getBeginDate());
        }

        if(Validator.isNotEmpty(cwFinanceCash.getEndDate())){
            queryWrapper.le(CwFinanceCash::getPaidTime,cwFinanceCash.getEndDate());
        }

        //结算方式
        if(Validator.isNotEmpty(cwFinanceCash.getSettelType())){
            queryWrapper.eq(CwFinanceCash::getSettelType,cwFinanceCash.getSettelType());
        }

        //封装
        if(Validator.isNotEmpty(cwFinanceCash.getJournalPack())){
            queryWrapper.eq(CwFinanceCash::getJournalPack,cwFinanceCash.getJournalPack());
        }

        //收付款凭证
        if(Validator.isNotEmpty(cwFinanceCash.getPaymentVoucher())){
            queryWrapper.eq(CwFinanceCash::getPaymentVoucher,cwFinanceCash.getPaymentVoucher());
        }

        //特殊处理凭证
        if(Validator.isNotEmpty(cwFinanceCash.getSpecVoucher())){
            queryWrapper.eq(CwFinanceCash::getSpecVoucher,cwFinanceCash.getSpecVoucher());
        }

        //核销凭证
        if(Validator.isNotEmpty(cwFinanceCash.getHxVoucher())){
            queryWrapper.eq(CwFinanceCash::getHxVoucher,cwFinanceCash.getHxVoucher());
        }

        //日记账
        if(Validator.isNotEmpty(cwFinanceCash.getJournalNote())){
            queryWrapper.eq(CwFinanceCash::getJournalNote,cwFinanceCash.getJournalNote());
        }

        //核销凭证2
        if(Validator.isNotEmpty(cwFinanceCash.getHxVoucher2())){
            queryWrapper.eq(CwFinanceCash::getHxVoucher2,cwFinanceCash.getHxVoucher2());
        }

        //开户银行
        if(Validator.isNotEmpty(cwFinanceCash.getBankName())){
            queryWrapper.eq(CwFinanceCash::getBankName,cwFinanceCash.getBankName());
        }

        //经办人
        if(Validator.isNotEmpty(cwFinanceCash.getUserId())){
            queryWrapper.eq(CwFinanceCash::getUserId,cwFinanceCash.getUserId());
        }

        //收入付出类型：0收入 1付出
        if(Validator.isNotEmpty(cwFinanceCash.getRpFlag())){
            queryWrapper.eq(CwFinanceCash::getRpFlag,cwFinanceCash.getRpFlag());
        }

        //是否预付款
        if(Validator.isNotEmpty(cwFinanceCash.getIsAdvance())){
            queryWrapper.eq(CwFinanceCash::getIsAdvance,cwFinanceCash.getIsAdvance());
        }

        //状态
        if(Validator.isNotEmpty(cwFinanceCash.getStatus())){
            queryWrapper.eq(CwFinanceCash::getStatus,cwFinanceCash.getStatus());
        }

        //创建人
        if(Validator.isNotEmpty(cwFinanceCash.getCreateBy())){
            queryWrapper.eq(CwFinanceCash::getCreateBy,cwFinanceCash.getCreateBy());
        }

        //创建时间
        if(Validator.isNotEmpty(cwFinanceCash.getCreateTime())){
            queryWrapper.eq(CwFinanceCash::getCreateTime,cwFinanceCash.getCreateTime());
        }

        //修改人
        if(Validator.isNotEmpty(cwFinanceCash.getUpdateBy())){
            queryWrapper.eq(CwFinanceCash::getUpdateBy,cwFinanceCash.getUpdateBy());
        }

        //修改时间
        if(Validator.isNotEmpty(cwFinanceCash.getUpdateTime())){
            queryWrapper.eq(CwFinanceCash::getUpdateTime,cwFinanceCash.getUpdateTime());
        }

        //删除标志（0代表存在 2代表删除）
        if(Validator.isNotEmpty(cwFinanceCash.getDelFlag())){
            queryWrapper.eq(CwFinanceCash::getDelFlag,cwFinanceCash.getDelFlag());
        }

        //租户id
        if(Validator.isNotEmpty(cwFinanceCash.getTenantId())){
            queryWrapper.eq(CwFinanceCash::getTenantId,cwFinanceCash.getTenantId());
        }

        //备注
        if(Validator.isNotEmpty(cwFinanceCash.getRemark())){
            queryWrapper.eq(CwFinanceCash::getRemark,cwFinanceCash.getRemark());
        }

        }

}
