package com.jianshui.income.mapper;


import com.jianshui.income.domain.entity.InvoiceCheck;
import com.jianshui.income.domain.entity.InvoiceCheckEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * InvoiceCheck的Mapper注解
 */
@Mapper
public interface InvoiceCheckMapper {

    @Select("select * from invoice_check where company_id = #{company_id}")
    public List<InvoiceCheck> selectByCompanyId(@Param("company_id") String companyId);

    /**
     * 根据发票代码和发票号码查询发票是否存在
     */
    @Select("select * from invoice_check where company_id = #{company_id} and fpdm = #{fpdm} and fphm = #{fphm} limit 1")
    public InvoiceCheck selectInvoiceCheckByFpdmAndFphm(@Param("company_id") String companyId, @Param("fpdm") String fpdm, @Param("fphm") String fphm);

    /**
     * 插入数据库
     *
     * @param invoiceCheck
     * @return
     */
    @Insert({
            "replace into invoice_check (id,company_id,cyjg,cyjgxx,fpzl,fpdm,fphm,cycs,xhfmc,xhfsbh,xhfdzdh,xhfyhzh,gmfsbh,gmfmc,gmfdzdh,gmfyhzh,kprq,fpje,fpse,jshj,bz,jqbh,kpr,skr,fhr,jym,zfbz)",
            " values (NULL,#{company_id},#{cyjg},#{cyjgxx},#{fpzl},#{fpdm},#{fphm},#{cycs},#{xhfmc},#{xhfsbh},#{xhfdzdh},#{xhfyhzh},#{gmfsbh},#{gmfmc},#{gmfdzdh},#{gmfyhzh},#{kprq},#{fpje},#{fpse},#{jshj},#{bz},#{jqbh},#{kpr},#{skr},#{fhr},#{jym},#{zfbz})"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id") // 通过这一句，可以将插入后的自增id放到id里
    public int insertInvoice(InvoiceCheck invoiceCheck);

    /**
     * 批量插入invoiceCheck
     *
     * @param invoiceCheckList
     * @return
     */
    @Insert(
            "<script> insert into invoice_check (id,company_id,pch,cyjg,cyjgxx,fpzl,fpdm,fphm,cycs,xhfmc,xhfsbh,xhfdzdh,xhfyhzh,gmfsbh,gmfmc,gmfdzdh,gmfyhzh,kprq,fpje,fpse,jshj,bz,jqbh,kpr,skr,fhr,jym,zfbz)"
                    + "VALUES "
                    + "<foreach collection='list' item='record' separator=',' > "
                    + "(NULL,#{record.company_id},#{record.pch},#{record.cyjg},#{record.cyjgxx},#{record.fpzl},#{record.fpdm},#{record.fphm},#{record.cycs},#{record.xhfmc},#{record.xhfsbh},#{record.xhfdzdh},#{record.xhfyhzh},#{record.gmfsbh},#{record.gmfmc},#{record.gmfdzdh},#{record.gmfyhzh},#{record.kprq},#{record.fpje},#{record.fpse},#{record.jshj},#{record.bz},#{record.jqbh},#{record.kpr},#{record.skr},#{record.fhr},#{record.jym},#{record.zfbz})"
                    + "</foreach>"
                    + "</script>"
    )
    public int insertInvoiceBatch(List<InvoiceCheck> invoiceCheckList);

    /**
     * 根据id更新invoice_check
     *
     * @param invoiceCheck
     * @return
     */
    @Update({
            "update invoice_check " +
                    "set `company_id` = #{company_id} , " +
                    "`cyjg` = #{cyjg} , " +
                    "`cyjgxx` = #{cyjgxx} , " +
                    "`fpzl` = #{fpzl} , " +
                    "`fpdm` = #{fpdm} , " +
                    "`fphm` = #{fphm} , " +
                    "`cycs` = #{cycs} , " +
                    "`xhfmc` = #{xhfmc} , " +
                    "`xhfsbh` = #{xhfsbh} , " +
                    "`xhfdzdh` = #{xhfdzdh} , " +
                    "`xhfyhzh` = #{xhfyhzh} , " +
                    "`gmfsbh` = #{gmfsbh} , " +
                    "`gmfmc` = #{gmfmc} , " +
                    "`gmfdzdh` = #{gmfdzdh} , " +
                    "`gmfyhzh` = #{gmfyhzh} , " +
                    "`kprq` = #{kprq} , " +
                    "`fpje` = #{fpje} , " +
                    "`fpse` = #{fpse} , " +
                    "`jshj` = #{jshj} , " +
                    "`bz` = #{bz} , " +
                    "`jqbh` = #{jqbh} , " +
                    "`kpr` = #{kpr} , " +
                    "`skr` = #{skr} , " +
                    "`fhr` = #{fhr} , " +
                    "`jym` = #{jym} , " +
                    "`zfbz` = #{zfbz} , " +
                    "`status` = #{status} " +
                    "where `id` = #{id}"
    })
    public int updateInoviceById(InvoiceCheck invoiceCheck);

    /**
     * 根据公司id和批次号批量查出结果
     *
     * @param companyId
     * @param pch
     * @return
     */
    @Select("select * from invoice_check where company_id = #{companyId} and pch = #{pch}")
    public List<InvoiceCheck> selectInvoiceCheckByPchAndCompanyId(@Param("companyId") String companyId, @Param("pch") String pch);

    /**
     * 批量更新invoiceCheck
     *
     * @param invoiceCheckList
     * @return
     */
    @Update("<script>"
            + "<foreach collection=\"list\" item=\"record\" separator=\";\" open=\"\" close=\"\">"
            + "update invoice_check set "
            + "cyjg = #{record.cyjg},"
            + "cyjgxx = #{record.cyjgxx},"
            + "fpzl = #{record.fpzl},"
            + "fpdm = #{record.fpdm},"
            + "fphm = #{record.fphm},"
            + "cycs = #{record.cycs},"
            + "xhfmc = #{record.xhfmc},"
            + "xhfsbh = #{record.xhfsbh},"
            + "xhfdzdh = #{record.xhfdzdh},"
            + "xhfyhzh = #{record.xhfyhzh},"
            + "gmfsbh = #{record.gmfsbh},"
            + "gmfmc = #{record.gmfmc},"
            + "gmfdzdh = #{record.gmfdzdh},"
            + "gmfyhzh = #{record.gmfyhzh},"
            + "kprq = #{record.kprq},"
            + "fpje = #{record.fpje},"
            + "fpse = #{record.fpse},"
            + "jshj = #{record.jshj},"
            + "bz = #{record.bz},"
            + "jqbh = #{record.jqbh},"
            + "kpr = #{record.kpr},"
            + "skr = #{record.skr},"
            + "fhr = #{record.fhr},"
            + "jym = #{record.jym},"
            + "zfbz = #{record.zfbz},"
            + "status = #{record.status}"
            + " where id = #{record.id} "
            + "</foreach>"
            + "</script>")
    public int updateInvoiceCheckBatch(List<InvoiceCheck> invoiceCheckList);


    /**
     * 判断批次号是否存在
     *
     * @param companyId
     * @param pch
     * @return
     */
    @Select("select count(1) from invoice_check where company_id = #{companyId} and pch = #{pch}")
    public int isPchExist(@Param("companyId") String companyId, @Param("pch") String pch);


    /**
     * 根据fpdm和Fphm获得七天之内查验成功的发票数据
     *
     * @param invoiceCheckList
     * @param date
     * @return
     */
    @Select("<script>"
            + "select * from invoice_check "
            + "<where>"
            + "<foreach collection=\"list\" item=\"record\" separator=\" or \" open=\"(\" close=\")\">"
            + "fpdm = #{record.fpdm} and fphm = #{record.fphm}"
            + "</foreach>"
            + " and  created_at >= #{date} and company_id = #{companyId} and cyjg is not null"
            + "</where>"
            + " order by created_at desc "
            + "</script>")
    public List<InvoiceCheck> selectInvoiceCheckByFpdmAndFphmWithouPch(@Param("list") List<InvoiceCheck> invoiceCheckList, @Param("date") String date,@Param("companyId") String companyId);


    List<InvoiceCheckEntity> selectInvoiceCheckListInfo(InvoiceCheckEntity invoiceCheck);

    @Insert({
            "replace into invoice_check (id,company_id,pch,cyjg,cyjgxx,fpzl,fpdm,fphm,cycs,xhfmc,xhfsbh,xhfdzdh,xhfyhzh,gmfsbh,gmfmc,gmfdzdh,gmfyhzh,kprq,fpje,fpse,jshj,bz,jqbh,kpr,skr,fhr,jym,zfbz)",
            " values (#{id},#{company_id},#{pch},#{cyjg},#{cyjgxx},#{fpzl},#{fpdm},#{fphm},#{cycs},#{xhfmc},#{xhfsbh},#{xhfdzdh},#{xhfyhzh},#{gmfsbh},#{gmfmc},#{gmfdzdh},#{gmfyhzh},#{kprq},#{fpje},#{fpse},#{jshj},#{bz},#{jqbh},#{kpr},#{skr},#{fhr},#{jym},#{zfbz})"
    })
    public int replaceInvoiceCheckBatch(InvoiceCheck invoiceCheck);

    @Select("select count(1) from invoice_check where pch = #{pch}")
    public int countPch( @Param("pch") String pch);
}
