package com.iformall.mapper;


import com.iformall.domain.po.MallPermission;
import com.iformall.domain.po.MallRolePermission;
import com.iformall.domain.po.MallSaleType;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;

import java.util.List;

@Mapper
public interface MallMenuMapper {
    @UpdateProvider(type = MallPermissionDaoProvider.class, method = "updateMallPermission")
    public void updateMallPermission(MallPermission mallPermission);

    @UpdateProvider(type = MallPermissionDaoProvider.class, method = "updatePermissionByParentId")
    public void updatePermissionByParentId(MallPermission mallPermission);

    @DeleteProvider(type = MallPermissionDaoProvider.class, method = "deletePermissionRole")
    public void deletePermissionRole(MallRolePermission mallRolePermission);

    @UpdateProvider(type = MallPermissionDaoProvider.class, method = "updateMallSaleType")
    public void updateMallSaleType(MallSaleType mallSaleType);

    @SelectProvider(type = MallPermissionDaoProvider.class, method = "getMallRolePermissions")
    public List<MallRolePermission> getMallRolePermissions(MallRolePermission mallRolePermission);

    class MallPermissionDaoProvider {
        public String updateMallPermission(MallPermission mallPermission) {
            String sql = "UPDATE mall_permission ";
            if (mallPermission.getName() != null) {
                sql += " SET `name` = #{name}";
            }
            if (mallPermission.getId() != null) {
                sql += " where id = #{id}";
            }
            return sql;
        }
        public String updatePermissionByParentId(MallPermission mallPermission) {
            StringBuilder sb = new StringBuilder();
            sb.append("UPDATE mall_permission ");
            if (mallPermission.getParentId() != null) {
                sb.append(" SET `parent_id` = #{parentId} ");
            }
            if (mallPermission.getOrigParentId() != null) {
                sb.append(" WHERE `parent_id` = #{origParentId}");
            }
            if (mallPermission.getIds() != null) {
                List ids = mallPermission.getIds();
                sb.append(" WHERE `id` in (");
                for(int i=0;i<ids.size();i++) {
                    sb.append(ids.get(i));
                    if (i < ids.size() - 1) {
                        sb.append(",");
                    }
                }
                sb.append(")");
            }
            return sb.toString();
        }
        public String updateMallSaleType(MallSaleType mallSaleType) {
            String sql = "UPDATE mall_sale_type ";
            if (mallSaleType.getMenus() != null) {
                sql += " SET `menus` = #{menus}";
            }
            if (mallSaleType.getId() != null) {
                sql += " where id = #{id}";
            }
            return sql;
        }
        public String deletePermissionRole(MallRolePermission mallRolePermission) {
            String sql = "DELETE FROM mall_role_permission ";
            if (mallRolePermission.getPermissionId() != null) {
                sql += " WHERE `permission_id` = #{permissionId}";
            }
            return sql;
        }
        public String getMallRolePermissions(MallRolePermission mallRolePermission) {
            StringBuilder sb = new StringBuilder();
            sb.append("SELECT * FROM mall_role_permission ");
            if (mallRolePermission.getIds() != null) {
                List ids = mallRolePermission.getIds();
                sb.append(" WHERE `permission_id` in (");
                for(int i=0;i<ids.size();i++) {
                    sb.append(ids.get(i));
                    if (i < ids.size() - 1) {
                        sb.append(",");
                    }
                }
                sb.append(")");
            }
            return sb.toString();
        }
    }
}
