/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ifish.mapper;

import com.ifish.bean.CommodityInfoBean;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;

/**
 *
 * @author Administrator
 */
public class Tbl_Commodity_Info_MapperSql {

    public String createCommodityInfo(@Param("commodity") CommodityInfoBean commodityInfoBean) {
        StringBuilder sb = new StringBuilder();

        sb.append("INSERT INTO tbl_commodity_info (shop_id,user_id,commodity_name,");
        if (StringUtils.isNotBlank(commodityInfoBean.getCommodityDetail())) {
            sb.append("commodity_detail,");
        }
        if (StringUtils.isNotBlank(commodityInfoBean.getCommodityImg())) {
            sb.append("commodity_img,");
        }
        if (StringUtils.isNotBlank(commodityInfoBean.getCommodityVideo())) {
            sb.append("commodity_video,");
        }
        sb.append("commodity_status,create_time,click,backstage_status) VALUES (#{commodity.shopId},#{commodity.userId},#{commodity.commodityName},");
        if (StringUtils.isNotBlank(commodityInfoBean.getCommodityDetail())) {
            sb.append("#{commodity.commodityDetail},");
        }
        if (StringUtils.isNotBlank(commodityInfoBean.getCommodityImg())) {
            sb.append("#{commodity.commodityImg},");
        }
        if (StringUtils.isNotBlank(commodityInfoBean.getCommodityVideo())) {
            sb.append("#{commodity.commodityVideo},");
        }
        sb.append("0,NOW(),0,0)");
        return sb.toString();
    }

    public String updateCommodityInfo(@Param("commodity") CommodityInfoBean commodityInfoBean) {
        StringBuilder sb = new StringBuilder();

        sb.append("UPDATE tbl_commodity_info SET ");
        if (commodityInfoBean.getShopId() != null && commodityInfoBean.getShopId() >= 0) {
            sb.append("shop_id = #{commodity.shopId},");
        }
        if (commodityInfoBean.getUserId() != null && commodityInfoBean.getUserId() >= 0) {
            sb.append("user_id = #{commodity.userId},");
        }
        if (commodityInfoBean.getClick() != null && commodityInfoBean.getClick() > 0) {
            sb.append("click = click+1 ,");
        }
        if (commodityInfoBean.getBackstageStatus() != null && commodityInfoBean.getBackstageStatus() >= 0) {
            sb.append("backstage_status = #{commodity.backstageStatus},");
        }
        if (commodityInfoBean.getCommodityStatus() != null && commodityInfoBean.getCommodityStatus() >= 0) {
            sb.append("commodity_status = #{commodity.commodityStatus},");
        }
        if (StringUtils.isNotBlank(commodityInfoBean.getCommodityName())) {
            sb.append("commodity_name = #{commodity.commodityName},");
        }
        if (StringUtils.isNotBlank(commodityInfoBean.getCommodityImg())) {
            sb.append("commodity_img = #{commodity.commodityImg},");
        }
        if (StringUtils.isNotBlank(commodityInfoBean.getCommodityDetail())) {
            sb.append("commodity_detail = #{commodity.commodityDetail},");
        }
        if (StringUtils.isNotBlank(commodityInfoBean.getCommodityVideo())) {
            sb.append("commodity_video = #{commodity.commodityVideo},");
        }
        if (sb.lastIndexOf(",") == sb.length() - 1) {
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append(" WHERE commodity_id = #{commodity.commodityId}");
        return sb.toString();
    }
}
