<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="vip.xiaonuo.purchase.mapper.PurchaseOrderMapper">


    <select id="listType" resultType="vip.xiaonuo.purchase.entity.vo.PurchaseOrderTypeVo">
        select material as 'material',
        IFNULL(sum(case when create_time >=#{month} then purchase_money else 0 end),'0') as 'monthMoney',
        IFNULL(sum(case when create_time >=#{year} then purchase_money else 0 end),'0') as 'yearMoney',
        IFNULL(sum(case when create_time between '${lastMonth}-01 00:00:00' and '${lastMonth}-31 23:59:59' then purchase_money else 0 end),'0') as 'lastMonthMoney',
        IFNULL(sum(case when create_time between '${lastYear}-01-01 00:00:00' and '${lastYear}-12-31 23:59:59' then purchase_money else 0 end),'0') as 'lastYearMoney'
        from purchase_order
        WHERE  material IS NOT NULL
        <if test="orgId!=null and orgId!=''">
            and org_id=#{orgId}
        </if>
        group by material
    </select>

    <select id="getSupplier" resultType="vip.xiaonuo.purchase.entity.vo.PurchaseMaterialVo">
        select IFNULL(sum(purchase_money),'0') as 'value',supplier as 'name' from purchase_order
        ${ew.customSqlSegment}
        group by supplier
    </select>
    <select id="purchaseAll" resultType="vip.xiaonuo.purchase.param.PurchaseAllParam">
 SELECT
    ROUND(IFNULL(SUM(purchase_money) , 0)/10000, 2) AS caiGouMoney,

    ROUND(IFNULL( (SELECT SUM(purchase_money)
     FROM `purchase_order`  ${ew.customSqlSegment}   AND  material ='羊毛粗脂'), 0)/10000, 2) AS zhuYaoMoney ,

      ROUND(IFNULL( (SELECT SUM(cai_gou)
     FROM `purchase_order`    ${ew.customSqlSegment}   AND  material ='羊毛粗脂'), 0), 2) AS zhuYaoNum,

    ROUND(IFNULL(SUM(invoice_price), 0)/10000, 2) AS yvQiYingFu,

      ROUND(IFNULL( (SELECT SUM(kuncun_wuliao_money)
     FROM  `warehouse_kucun_money`   ${ew.customSqlSegment}    ), 0)/10000, 2) AS kuCunMoney,

     ROUND(IFNULL( (SELECT SUM(zhuyao_wuliao_money)
     FROM  `warehouse_kucun_money`   ${ew.customSqlSegment}   ), 0)/10000, 2) AS zhuYaoYuanCaiLiaoMoney,


      ROUND(IFNULL( (SELECT SUM(material_number )
     FROM  `warehouse_stock_detail`    ${ew.customSqlSegment}   AND   material_name ='羊毛粗脂'  ), 0), 2) AS zhuYaoYuanCaiLiaoNum
   FROM
    `purchase_order`
     ${ew.customSqlSegment}

    </select>
    <select id="getCaiGouYvFuDetailMonth" resultType="vip.xiaonuo.purchase.param.PurchaseAllParam">
           SELECT
    ROUND(IFNULL(SUM(purchase_money ) , 0) / 10000, 2) AS caiGouMoney,
    ROUND(IFNULL(SUM(CASE WHEN material = '羊毛粗脂' THEN purchase_money ELSE 0 END),0) / 10000 , 2) AS zhuYaoMoney,
    ROUND(IFNULL(SUM(CASE WHEN material = '羊毛粗脂' THEN cai_gou ELSE 0 END), 2)) AS zhuYaoNum,
    ROUND(IFNULL(SUM(invoice_price) , 0)/ 10000, 2) AS yvQiYingFu,
    DATE_FORMAT(create_time, '%Y-%m') AS ymonth
FROM
    `purchase_order`
 ${ew.customSqlSegment}



    </select>
    <select id="getCaiGouYvFuDetailYear" resultType="vip.xiaonuo.purchase.param.PurchaseAllParam">
          SELECT
    ROUND(IFNULL(SUM(purchase_money ) , 0) / 10000, 2) AS caiGouMoney,
    ROUND(IFNULL(SUM(CASE WHEN material = '羊毛粗脂' THEN purchase_money ELSE 0 END),0) / 10000 , 2) AS zhuYaoMoney,
    ROUND(IFNULL(SUM(CASE WHEN material = '羊毛粗脂' THEN cai_gou ELSE 0 END), 2)) AS zhuYaoNum,
    ROUND(IFNULL(SUM(invoice_price) , 0)/ 10000, 2) AS yvQiYingFu,
    DATE_FORMAT(create_time, '%Y') AS ymonth
FROM
    `purchase_order`
 ${ew.customSqlSegment}

    </select>
</mapper>
