<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zioinfo.mall.store.mapper.StoreMapper">
  <select id="findAll" resultType="com.zioinfo.mall.store.MallStore">
    SELECT * FROM mall_store
    <where><if test="activeOnly == true">active = TRUE</if></where>
    ORDER BY code
  </select>
  <select id="findById" resultType="com.zioinfo.mall.store.MallStore">SELECT * FROM mall_store WHERE id = #{id}</select>
  <select id="findByCode" resultType="com.zioinfo.mall.store.MallStore">SELECT * FROM mall_store WHERE code = #{code}</select>
  <insert id="insert" parameterType="com.zioinfo.mall.store.MallStore" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO mall_store (code, name, city, state, zip, address, phone, timezone, open_time, close_time,
                            same_day_cutoff, delivery_radius_mi, daily_capacity, lat, lng, active)
    VALUES (#{code}, #{name}, #{city}, #{state}, #{zip}, #{address}, #{phone}, COALESCE(#{timezone},'America/New_York'),
            COALESCE(#{openTime},'09:00'), COALESCE(#{closeTime},'18:00'), COALESCE(#{sameDayCutoff},'12:00'),
            COALESCE(#{deliveryRadiusMi},15.0), COALESCE(#{dailyCapacity},50), #{lat}, #{lng}, COALESCE(#{active},TRUE))
  </insert>
  <update id="update">
    UPDATE mall_store SET name=#{name}, city=#{city}, state=#{state}, zip=#{zip}, address=#{address}, phone=#{phone},
      timezone=COALESCE(#{timezone},timezone), open_time=COALESCE(#{openTime},open_time), close_time=COALESCE(#{closeTime},close_time),
      same_day_cutoff=COALESCE(#{sameDayCutoff},same_day_cutoff), delivery_radius_mi=COALESCE(#{deliveryRadiusMi},delivery_radius_mi),
      daily_capacity=COALESCE(#{dailyCapacity},daily_capacity), lat=COALESCE(#{lat},lat), lng=COALESCE(#{lng},lng)
    WHERE id=#{id}
  </update>
  <update id="setActive">UPDATE mall_store SET active=#{active} WHERE id=#{id}</update>
</mapper>
